I have a script that validates form entries all with one page and it works fine with PHP4 but running it on my server with PHP5 it will not show errors like it is supposed to, and there are actually only three required fields so for testing I naturally leave out various combinations, none of them show up like they are supposed to and even when filling out the entire required fields it will not go to the done.php file for further processing.
It does however return with the Username and password field filled out like it is supposed to after they have been entered. Here is the entire code along with the HTML form code.
<html>
<body>
<h3>PHP User Registration Form Example</h3>
<?php
// only validate form when form is submitted
if(isset($submit_button)){
$error_msg='';
if(trim($username_input)=='' || strlen(trim($username_input)) < 6 || strlen(trim($username_input)) > 15) {
$error_msg.="Please enter a username between 6 to 15 characters long<br>";
}
if(trim($password_input)=='' || strlen(trim($password_input)) < 4) {
$error_msg.="Please enter a password at least 4 characters long<br>";
}
if(trim($email_input)=='') {
$error_msg.="Please enter an email<br>";
} else {
// check if email is a valid address in this format username@domain.com
if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>";
}
// display error message if any, if not, proceed to other processing
if($error_msg==''){
//other processs goes here like process the form to database.
header("location:done.php");
//exit;
} else {
echo "<font color=red>$error_msg</font>";
}
}
?>
<form method="POST" action="registration.php">
<table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width="16%" align="right">First Name</td>
<td width="84%">
<input type="text" name="first_name_input" size="20" value="<?php echo $first_name_input; ?>"></td>
</tr>
<tr>
<td width="16%" align="right">Last Name</td>
<td width="84%">
<input type="text" name="last_name_input" size="20" value="<?php echo $last_name_input; ?>"></td>
</tr>
<tr>
<td width="16%" align="right">Username</td>
<td width="84%">
<input type="text" name="username_input" size="20" value="<?php echo $username_input; ?>"> (between
6 to 15 characters)</td>
</tr>
<tr>
<td width="16%" align="right">Password</td>
<td width="84%">
<input type="password" name="password_input" size="20" value="<?php echo $password_input; ?>">
(must be at least 4 characters)</td>
</tr>
<tr>
<td width="16%" align="right">Email</td>
<td width="84%">
<input type="text" name="email_input" size="20" value="<?php echo $email_input; ?>"></td>
</tr>
<tr>
<td width="16%" align="right"> </td>
<td width="84%">
<input type="submit" value=" Register " name="submit_button"></td>
</tr>
</table>
</form>
</body>
</html>