Code for Artists - Miles DeCoster

Use PHP to add radio buttons



Male
Female

Morning
Afternoon
Evening
Night


This version adds a conditional to the function to see if the form has been submitted and if it has “remembers” the selections after the submission. It also remembers any values typed into the text imput form elements.

<?php 
	function make_radios($name, $value) {
		echo '<input type="radio" name="'.$name.'" value="'.$value.'"';
	if (isset($_POST[$name]) && ($_POST[$name] == $value)) {
		echo ' checked="checked"';
	} 
		echo " /> $value ";
	} // end of function
    
    // check to see if the text imputs have values and store if they do
    
    if (isset($_POST[fName])) {
		$fName = ($_POST[fName]);
	}
	if (isset($_POST[lName])) {
		$lName = ($_POST[lName]);
	}
?>

<form action="" method="post">
<label>First Name: </label><input name="fName" type="text" value="<?php echo $fName; ?>"><br>
<label>Last Name: </label><input name="lName" type="text" value="<?php echo $lName; ?>"><br>
<?php make_radios("Gender","Male"); ?>
<?php make_radios("Gender","Female"); ?>
<?php make_radios("time","Morning"); ?>
<?php make_radios("time","Afternoon"); ?>
<?php make_radios("time","Evening"); ?>
<?php make_radios("time","Night"); ?>
</form>