Radio buttons on a simple PHP form

Hello folks, I am brand new to the forum and very new to PHP so please be gentle. :slight_smile:

I have created a simple form where I collect some basic information from the user and check that data. The script checks for blanks and proper formatting in the fields with regular expressions. The good information is stored in two arrays called $clean_data[] and $good_data[]. If not all required fields are filled in the form is re-displayed but not before using the extraxt function and passing it the $clean_data and $good_data[] arrays so that the user does not need to fill in all fields again. This works to a degree, but I am not sure how to have the radio button the user selected re-checked when the form is re-displayed. I am doing a check on the radio button vs. a regular expression where only 4 options are available and if the data is good it is stored in $good_data, I assumed. If anyone happens to have a moment to smack me upside the head and tell me what I am doing wrong it would be greatly appreciated.

Here is my form:

<?php /* Program name: my-form.inc * Description: Defines a form that collects a user's * information. */ $labels = array( "first_name" => "First Name", "middle_name" => "Middle Name", "last_name" => "Last Name", "phone" => "Phone", "street" => "Street Address", "postal" => "Postal Code" ); $radios = array( "NS", "PEI", "NB", "NFLD" ); $submit = "Submit Information"; ?> <!-- form { margin: 1.5em 0 0 0; border : 1px solid #000; padding : 5px; background-color : #fff;
  }
  .field {padding-bottom: 1em;}
  label {
    font-weight: bold;
    float: left;
    width: 20%;
    margin-right: 1em;
    text-align: right;
  }
  .submit {
    margin-left: 35%;
  }

–>

Please enter your mailing address information below

<?php /* loop that displays the form */ echo ""; foreach($labels as $field => $label) { echo "
$label
\n"; }

echo "


$radios[0]
$radios[1]
$radios[2]
$radios[3]
";

echo “”;

echo "


";
?>

And here is my Check Script:

<?php /* Program name: checkMyForm.php * Description: Script displays a blank form and checks * all the form fields for blank fields and for legitimate content. */ if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") { foreach($_POST as $field => $value) { if(empty($value)) { if($field != "middle_name") { $blank_array[] = $field; } } else { $good_data[$field] = strip_tags(trim($value)); } } if(@sizeof($blank_array) > 0) { $message = "

You didn't fill in one or more required fields. You must enter:

    "; /* display list of missing information */ foreach($blank_array as $value) { $message .= "
  • $value
  • "; } $message .= "
"; echo $message; extract($good_data); include("my-form.inc"); exit(); } foreach($_POST as $field => $value) { if(!empty($value)) { $name_patt = "/^[A-Za-z' -]{1,50}$/"; $phone_patt = "/^[0-9)(xX -]{7,20}$/"; $addr_patt = "/^[A-Za-z0-9 .,'-]{1,50}$/"; $postal_patt = "/^[A-Za-z0-9]{6}$/"; $radio_patt = "/(NS|PEI|NB|NFLD)/"; if(preg_match("/name/i",$field)) { if(!preg_match($name_patt,$value)) { $error_array[] = "$value is not a valid name"; } } if(preg_match("/phone/i",$field)) { if(!preg_match($phone_patt,$value)) { $error_array[] = "$value is not a valid phone number"; } } // endif phone format check if(preg_match("/street/i",$field)) { if(!preg_match($addr_patt,$value)) { $error_array[] = "$value is not a valid street address"; } } if(preg_match("/postal/i",$field)) { if(!preg_match($postal_patt,$value)) { $error_array[] = "$value is not a valid postal code"; } } if(preg_match("/province/i",$field)) { if(!preg_match($radio_patt,$value)) { $error_array[] = "$value is not a valid province."; } } } $clean_data[$field] = strip_tags(trim($value)); } if(@sizeof($error_array) > 0) { $message = "
    "; foreach($error_array as $value) { $message .= "
  • $value
  • "; } $message .= "
"; echo $message; extract($clean_data); include("my-form.inc"); exit(); } else { echo "Data is all okay"; //$selected_radio = $_POST['province']; <--- just checking the value of province here //print $selected_radio; } } else { include("my-form.inc"); } ?>

Thanks in advance for any help !

-Bruce Campbell

I apologize for not pasting my code into the form correctly. :-[

-Bruce

Sponsor our Newsletter | Privacy Policy | Terms of Service