Forum Moderators: coopster
I have this form which is called via an include (see php code below) in register.php. Now the form works fine, in that required data is validated, stored in a database etc. If errors are found then the form is re-displayed with the errors shown.
The only issue I have in order to complete the form is to ensure that radio button and checkbox values that have been selected are re-displayed / made sticky when the form is re-displayed with errors. This has really stumped me. I have looked at using isset / arrays to pick up the values of a checkbox / radio button but to no avail.
Any help would be much appreciated.
<?php
$form_variables = array(
'guest_first_name' => array(
'regexp' => '/^.+$/',
'error' => '[First Name is Required]'
),
'guest_last_name' => array(
'regexp' => '/^.+$/',
'error' => '[Last Name is Required]'
),
'guest_email' => array(
'regexp' => '/^.+?@.+?\..+$/',
'error' => '[Valid E mail address is Required]'
)
);
function build_form($form_variables, $errors=array(), $data=array()) {
// Ensure $errors and $data have empty strings for incorrect fields
foreach (array_keys($form_variables) as $name) {
$data[$name] = isset($data[$name])? htmlspecialchars($data[$name]) : '';
if (!isset($errors[$name])) {
$errors[$name] = '';
} elseif ($errors[$name]) {
$data[$name] = '';
}
}
echo <<<EOD
<form action="$_SERVER[PHP_SELF]" method="post">
<fieldset id="section_01">
<legend class="required">Name <em>(Required)</em></legend>
<div class="field_item small">
<label for="guest_first_name">First <span class="errortxt">{$errors['guest_first_name']}</span></label>
<input type="text" id="guest_first_name" name="guest_first_name" value="{$data['guest_first_name']}" />
</div>
<div class="field_item small">
<label for="guest_last_name">Last <span class="errortxt">{$errors['guest_last_name']}</span></label>
<input type="text" id="guest_last_name" name="guest_last_name" value="{$data['guest_last_name']}" />
</div>
</fieldset>
<fieldset id="section_02">
<legend class="required">E mail <em>(Required)</em></legend>
<div class="field_item medium">
<label for="guest_email">E mail <span class="errortxt">{$errors['guest_email']}</span></label>
<input type="text" id="guest_email" name="guest_email" value="{$data['guest_email']}" />
</div>
</fieldset>
<fieldset id="section_03">
<legend class="required">Attendance <em>(Required)</em></legend>
<fieldset id="section_03A">
<legend>Attendance</legend>
<div class="field_radio medium">
<label for="attendance_yes">I <em>WILL</em> be attending </label>
<input type="radio" id="attendance_yes" name="guest_attendance" value="yes" />
</div>
<div class="field_radio medium">
<label for="attendance_no">I <em>WILL NOT</em> be attending</label>
<input type="radio" id="attendance_no" name="guest_attendance" value="no" />
</div>
</fieldset>
<fieldset id="section_03b" rel="show_attending_ceremony">
<legend>Coach Reservation</legend>
<p>Please specify if you will require a seat on the Coach:</p>
<div class="field_checkbox medium">
<label for="guest_coach_in">A to B - Coach Leaving at 9:00</label>
<input type="checkbox" id="guest_coach_in" name="guest_coach_in" value="yes" />
</div>
<div class="field_checkbox medium">
<label for="guest_coach_out">B to A - Coach Leaving at 18:00</label>
<input type="checkbox" id="guest_coach_out" name="guest_coach_out" value="yes" />
</div>
</fieldset>
</fieldset>
<input type="submit" value="Submit Details" />
</form>
EOD;
}
// Now generate the form or POST response page
// On submit check the form for errors
if ($_POST) {
$errors = array();
foreach ($form_variables as $name => $properties) {
$value = isset($_POST[$name])? $_POST[$name] : '';
if (!preg_match($properties['regexp'], $value)) {
$errors[$name] = $properties['error'];
}
}
if ($errors) {
// Redisplay the form if errors are found
// Display all errors above the form
echo '<div class="formerror">'."\n";
echo '<h3>There was a problem with your submission, please check the following form fields:</h3>'."\n";
echo '<ul id="error_list">'."\n";
foreach($errors as $error) echo '<li> '.$error.'</li>'."\n";
echo '</ul>'."\n";
echo '</div>'."\n";
// Show form with errors highlighted
echo build_form($form_variables, $errors, $_POST);
} else {
// Open database connection
include('dbConn.inc.php');
// Select database
if (!mysql_select_db('db_table')) {
exit('<p>Unable to locate the db' . 'database at this time.</p>');
}
// Data cleaning function
function clean_data($string) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
$string = strip_tags($string);
return mysql_real_escape_string($string);
}
// Pick up cleaned form data
$field1 = clean_data($_POST['guest_title']);
$field2 = clean_data($_POST['guest_first_name']);
$field3 = clean_data($_POST['guest_last_name']);
// etc ...
// Insert data
$query = "INSERT INTO table (field1, field2, field3 ....) VALUES ('$field1','$field2','$field3' ...)";
if (!mysql_query($query))
{
error_log(mysql_error());
echo '<p><em>Unfortunately, something went wrong with your form submission. Please try again.</strong></p>';
}
}
} else {
echo build_form($form_variables);
}
?>
set the selected status first by default
$attendance_yes = 'unchecked';
$attendance_no = 'unchecked';
if (isset($_POST['submit'])) {
$selected_radiobtn = $_POST['guest_attendance'];
if ($selected_radiobtn == 'yes') {
$attendance_yes = 'checked';
}elseif($selected_radiobtn == 'no') {
$attendance_no = 'checked';
}
}
then in your form put the following:
<input type="radio" id="attendance_yes" name="guest_attendance" value="yes" <?php echo $attendance_yes;?> />
<input type="radio" id="attendance_no" name="guest_attendance" value="no" <?php echo $attendance_no;?> />
Hope that helps
I am still unable to pass the result of $attendance_yes or $attendance_no back into the re-generated form when errors are detected.
You suggested the following to go into the form:
<input type="radio" id="attendance_yes" name="guest_attendance" value="yes" <?php echo $attendance_yes;?> />
Unfortunately this doesn't work within my form. Maybe its down to the way in which I have set up the form? Not sure.
I know the values of 'checked' or 'unchecked' are being set correctly as I have modded the form to print the contents of $attendance_yes and $attendance_no.
My current set-up is I have a file called form.php (which contains the html and within which is an include "<?php include('process.inc.php');?>", process.inc.php contains the code posted above.
Any further advice would be appreciated. Many thanks.
You need to put it in the buildform function is something like:
function build_form($form_variables, $errors=array(), $data=array()) {
// Ensure $errors and $data have empty strings for incorrect fields
foreach (array_keys($form_variables) as $name) {
$data[$name] = isset($data[$name])? htmlspecialchars($data[$name]) : '';
if (!isset($errors[$name])) {
$errors[$name] = '';
}elseif ($errors[$name]) {
$data[$name] = '';
}
}
set the selected status first by default
$attendance_yes = 'unchecked';
$attendance_no = 'unchecked';
$selected_radiobtn = $data['guest_attendance'];
if ($selected_radiobtn == 'yes') {
$attendance_yes = 'checked';
}elseif($selected_radiobtn == 'no') {
$attendance_no = 'checked';
}
## ECHO FORM
} // End of Function