Page 1 of 1

having problems with a radiobuttons and foreach function...

Posted: Wed Jun 16, 2004 1:30 pm
by Xplosion84

Code: Select all

<?php
if ($_SESSION['check'] == 1) {
	// arrays hold the field names
	$fields = array(Category , Subcategory , ProductName , Manufacturers , Descriptions); 
	$radio = array(InstockCheck , WeightCheck);
	$numeric = array(ProductNumber , Quanity , OurCost , SellPrice , TaxPrice , Dementions , Weight);
	
	//error variables
	$alphaerror = '';
	$numerror = '';
	$checkerror = '';
			
	// foreach sets the field name to $field and sets the value of that field to $value.
	//Also checks to see if the proper characters are in the fields.
	foreach($_POST as $field=>$value) {
		echo $field. $value. '<br>'; //used for coding purpouses to see if the fields are being set to $field and $value
		if ( in_array($field, $fields) && strlen($value) == 0 ) {
			$alphaerror .= $field.' ';
		} elseif ( in_array($field, $numeric) && !is_numeric($value) ) {
				$numerror .= $field.' ';
		  } elseif ( in_array($field, $radio) && $value == '') {
				$checkerror .= $field.' ';
		    }
	}
	
	//if the error variables are not set add the following to the beginning of the already set value of the error variables.
	if ( strlen($alphaerror) != 0 ) { $alphaerror = 'Please complete the following field(s) in alphanumeric form: '.$alphaerror; }
	if ( strlen($numerror) != 0 ) { $numerror = 'Please complete the following field(s) in numeric form: '.$numerror; }
	if ( strlen($checkerror) != 0 ) { $checkerror = 'Please check the following checkbox(s): '.$checkerror; }

}
?>
I'm having a hard time figureing out how to check to see if the radiobuttons are checked. What's happening is if I do not check a radiobutton the the field name of that radiobutton is not getting set to the $field variable and the value is not getting set to $value. It's like the radiobutton doesn't exsist(I used echo $field. $value. '<br>'; to print out the $field and $value to see if they are getting set) The reason why I said it seems like the radiobuttons don't exsist is because I don't recieve and print out of the $field and $value unless they are selected . Now, if I check a radiobutton the variables get set. Why is this happening? I'm stuck on how to see if the radiobutton is checked or not. Can anyone help me out with this and help me get this to work?

-Thanks
?>

Posted: Wed Jun 16, 2004 1:34 pm
by feyd
if no radio buttons are selected, the entire set is not passed by the browser As there's nothing to send. If the field exists.. guess what.. something is selected. Use the values of each radio button tell you which is selected.

Posted: Wed Jun 16, 2004 1:39 pm
by Xplosion84
feyd wrote:if no radio buttons are selected, the entire set is not passed by the browser As there's nothing to send. If the field exists.. guess what.. something is selected. Use the values of each radio button tell you which is selected.
So, the value will still be passed? Could you give me an example of how this will work?

Posted: Wed Jun 16, 2004 1:40 pm
by feyd
if something is selected, yes.

Posted: Wed Jun 16, 2004 1:49 pm
by Xplosion84
feyd wrote:if something is selected, yes.
nvm, i'm just goin to set a default check on one of them, thanks for the help

Posted: Wed Jun 16, 2004 1:55 pm
by feyd

Code: Select all

$radioVal = (isset($_POST['radiobutton'])?$_POST['radiobutton']:'-');
given a radiobutton set named 'radiobutton', if one is selected $radioVal will have the value selected, otherwise it'll have a dash..