Code for Artists - Miles DeCoster

Use PHP to add radio buttons



This version adds a conditional to the "make radios" 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. The next set of radio buttons and checkboxes are hard-coded in the html rather than php generated, but also remember their state when submitted.

	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> Example of hard-coded html radio input: <input type="radio" name="radio1" value="1" <?php echo (!empty($_POST['radio1'])&&$_POST['radio1']==1)?'checked':''?>> <label>Radio group</label><