Problem with validation in code
Posted: Wed Mar 15, 2006 9:17 am
Pimptastic | Please use

Pimptastic | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi I have created a script that displays a form, takes information and enters the staff details into a mysql database this all works fine except that the validation that is supposed to check that there are no blank fields or letters in the phone number ect. Seems to execute when the page is initially loaded instead of when the submit button is pressed. When the form loads it immediately says that there are blank field that must be filled and if there are blank field when submit is pressed there blank field is entered and not stopped by the warning message. Does anyone know of a solution to get this section of code to execute only once the submit button is pressed?
Thanks for any help you can give much appreciated.
The code is shown below:Code: Select all
<?php
/*Staff_details.php
*A Program to collect the staff details and enter them into the
*database using the Entry.php script.
*/
?>
<html>
<head><title>Staff Details Entry</title></head>
<body>
<?php
$firstname = strip_tags(trim($_POST['firstname']));
$lastname = strip_tags(trim($_POST['lastname']));
$department = strip_tags(trim($_POST['department']));
$roomnumber = strip_tags(trim($_POST['roomnumber']));
$phonenumber = strip_tags(trim($_POST['phonenumber']));
/* set up array of field labels */
$label_array = array (
"firstname" => "First Name",
"lastname" => "Last Name",
"department" => "Department",
"roomnumber" => "Room Number",
"phonenumber" => "Phone Number");
foreach ($_POST as $field => $value);
{
/* check each field for blank fields */
if ( $value == "" )
{
$blank_array[$field] = "blank";
}
elseif ( ereg("(lastname)",$field) )
{
if (!ereg("^[A-Za-z' -]{150}$",$_POST[$field]) )
{
$bad_format[$field] = "bad";
}
}
elseif ($field == "phonenumber")
{
if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
{
$bad_format[$field] = "bad";
}
}
} // end of foreach for $_POST
/* if any fields were not okay, display error message and form */
if (@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
{
if (@sizeof($blank_array) > 0)
{
/* display message for missing information */
echo "<b>You didn't fill in one or more required fields.
You must enter:</b><br>";
/* display list of missing information */
foreach($blank_array as $field => $value)
{
echo " {$label_array[$field]}<br>";
}
}
if (@sizeof($bad_format) > 0)
{
/* display message for bad information */
echo "<b>One or more fields have information that appears to
be incorrect. Correct the format for:</b><br>";
/* display list of bad information */
foreach($bad_format as $field => $value)
{
echo " {$label_array[$field]}<br>";
}
}
echo "<p><hr>
<form action='entry.php' method='POST'>
<center>
<table width='95%' border='0' cellspacing='0' cellpadding='2'>
</tr>
<tr><td align='right'><B>{$label_array['firstname']}:</br></td>
<td><input type='text' name='firstname' size='65' maxlength='65'> </td>
</tr>
<tr><td align='right'><B>{$label_array['lastname']}:</B></td>
<td> <input type='text' name='lastname' size='65' maxlength='65'> </td>
</tr>
<tr><td align='right'><B>{$label_array['department']}:</B></td>
<td> <input type='text' name='department' size='65' maxlength='65'> </td>
</tr>
<tr><td align='right'><B>{$label_array['roomnumber']}:</B></td>
<td> <input type='text' name='roomnumber' size='65' maxlength='65'> </td>
</tr>
<tr><td align='right'><B>{$label_array['phonenumber']}:</B></td>
<td> <input type='text' name='phonenumber' size='65' maxlength='65'> </td>
</tr>
</table>
<p><input type='submit' value='Enter staff details'>
</form>
</center>";
}
?>
</body></html>Pimptastic | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]