Radio button issue in a form processing script

hello,

I found a tutorial script that works for me but it doesn’t seem to work well with radio buttons. I’m hoping someone could have a look at that script and help me get it working with the radiobutton.

the selection carrie over to the process.php script but cant seem to process the selection.
need 2 things:
1 process the selection.
2. in the event user enters incorrect information somewhere else i like to be able to go back to the form with the users original radio button selection. eg. Name: <input type=“text” name=“name” value=“<?php echo $name; ?>” > <br />

any help you can give me would be greatly appreciated.

index.php


<body>
<h3>Form testing</h3>
<?php echo $errorString; ?>
<br />
<form name="formprocess" method="POST" action="process.php">
	Name: <input type="text" name="name" value="<?php echo $name; ?>" > <br />
	eMail: <input type="text" name="email" value="<?php echo $email; ?>" > <br />
	Birthday:<input type="text" name="birthday" value="<?php echo $birthday; ?>" /><br />
	<br />
	<p> <input type="radio" name="radiobutton" value="1"> OptionOne </p>
	<p> <input type="radio" name="radiobutton" value="2"> OptionTwo </p>
	<p> <input type="radio" name="radiobutton" value="3"> OptionThree</p>
	
	<input type="submit" name="formSubmit" value="Submit">
</form>
</body>

process.php - form processing script


<?php
$allowedFields = array(
	'name',
	'email',
	'birthday',
	'radiobutton',
);

$requiredFields = array(
	'name',
	'email',
	'radiobutton',
);
echo $_POST['radiobutton'];
echo '<br />';
print_r($_POST);
//loop through the POST array
$fieldErrors = array();
foreach($_POST as $key=>$value)
{	// allowed fields
	if(in_array($key, $allowedFields))
	{	$$key = $value;
		// required field?
		if(in_array($key, $requiredFields) && $value == '')
		{	$fieldErrors[] = "The field $key is required.";
		}
	}	
}

// Any errors
if(count($fieldErrors) > 0)
{	$errorString = '<p>There was an error processing the form.</p>';
	$errorString . 'ul>';
	foreach($fieldErrors as $errorVal)
	{	$errorString .= "<li>$errorVal</li>"; }
	$errorString .= '</ul>';
	include 'form.php';
}
else
{	echo "<br /> append to  database, goes here";  }
?>

hi robin,

this will do what you are trying to achieve


<body>
<h3>Form testing</h3>
<?php echo $errorString; ?>
<br />
<form name="formprocess" method="POST" action="process.php">
    Name: <input type="text" name="name" value="<?php echo $name; ?>" > <br />
    eMail: <input type="text" name="email" value="<?php echo $email; ?>" > <br />
    Birthday:<input type="text" name="birthday" value="<?php echo $birthday; ?>" /><br />
    <br />
    <p> <input type="radio" name="radiobutton" value="1" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 1) ?  'checked':''; ?>> OptionOne </p>
    <p> <input type="radio" name="radiobutton" value="2" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 2) ?  'checked':''; ?>> OptionTwo </p>
    <p> <input type="radio" name="radiobutton" value="3" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 3) ?  'checked':''; ?>> OptionThree</p>

    <input type="submit" name="formSubmit" value="Submit">
</form>
</body>

Thank you for the help. The script you provided works but in the process.php script the variable is passed but if the radio button isnt selected it bypasses the mandatory check.

If the radiobutton isnt selected the array is empty, the key value doesnt even appear any way to fix that?

f the radioButton isn’t set, set a default value before the code snippet you provided runs. That way one of the buttons will be checked.