I have two forms that uses PHP validation, then if there is no error, it goes straight to the next page using a javascript redirect.
One of my forms works perfectly, picking up errors and displaying a message, then redirecting when there is no error. However, the other form goes straight to the next page, even when there is an error.
The code for both:
join2.php (the one that works):
<?
// only validate form when form is submitted
if(isset($_POST["Submit"])){
$error_msg='';
if(trim($_POST["first"])=='') {
$error_msg.="Please enter a first name<br>";
}
if(trim($_POST["last"])=='') {
$error_msg.="Please enter a last name<br>";
}
if(trim($_POST["pass"])=='' || strlen(trim($_POST["pass"])) < 4) {
$error_msg.="Please enter a password at least 4 characters long<br>";
}
if(trim($_POST["email"])=='') {
$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]", $_POST["email"])) $error_msg.="Please enter a valid email address<br>";
}
// display error message if any, if not, proceed to other processing
if($error_msg==''){
// other process here
$first=$_POST[first];
$last=$_POST[last];
$email=$_POST[email];
$password = md5("$_POST[pass]");
$passemail=$_POST[pass];
$vatsimid=$_POST[vatsimid];
// Connect to the database server
$dbcnx = @mysql_connect("*****","***","***");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " . "database server at this time.</p>");
exit();
}
// Select the database
if (! @mysql_select_db("****", $dbcnx) ) {
echo( "<p>Unable to locate the joke " . "database at this time.</p>" );
exit();
}
// If submitted,
// add it to the database.
if ("Submit" == $Submit) {
$sql = "INSERT INTO roster SET " . "first_name='$first', " . "last_name='$last', " . "email='$email', " . "password='$password', " . "vatsimid='$vatsimid'";
if (mysql_query($sql)) {
//header('Location: thankyou.php');
// Get newest id
$pid=mysql_insert_id();
$to = "$email";
$subject = "Southern Cross Airlines Application";
$message = "Hello, $first.\n
I am pleased to inform you that you have been accepted into Southern Cross Airlines. Your details are as follows:\n
Pilot ID: $pid
Password: $passemail \n
You may now log in and file PIREPs when you complete your flight.\n
Please feel free to register at our forum and begin flying any route you choose.
I hope you enjoy your time at Southern Cross Airlines.
Owner/CEO\n\n
****This is an automated message, do not reply to this address****
";
$from = "admin@southerncross.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
?>
<SCRIPT language=JavaScript>
window.location="thankyou.php";
</SCRIPT>
<?php
}
else
{
echo("<p>Error adding submitted joke: " . mysql_error(). "</p>");
}
}
} else {
echo "<font color=red>$error_msg</font>";
}
}
?>
<form id="join" name="join" method="post" action="join2.php">
<table width="646" border="0">
<tr>
<td width="176"><label>First Name:</label></td>
<td width="460"><input name="first" type="text" id="first" size="20" maxlength="20" value="<? echo $first; ?>"/></td>
</tr>
<tr>
<td><label>Last Name:</label></td>
<td><input name="last" type="text" id="last" size="20" maxlength="20" value="<? echo $last; ?>" /></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td><input name="email" type="text" id="email" size="50" maxlength="50" value="<? echo $email; ?>"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input name="pass" type="password" id="pass" size="15" maxlength="15" value="<? echo $pass; ?>"/></td>
</tr>
<tr>
<td>If you are registered with VATSIM, enter your Vatsim ID</td>
<td><label>
<input name="vatsimid" type="text" id="vatsimid" size="10" maxlength="6" value="<? echo $vatsimid; ?>"/>
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="Submit" />
<input name="Reset" type="reset" id="Reset" value="Reset" />
</div>
</label></td>
</tr>
</table>
</form>