Hi,
Hope you guys are well, I'm looking for a small bit of help with the PHP side of my form. The form itself is working fine and when submitted, validates then emails the data. The problem I have is when I add in some redirection code that redirects the user to one of three pages, it causes the validation code to stop working, anyways here is the snippet im using. I just want to understand why the redirection code supercedes my validation code and how too stop it, many thanks.
<?
}
if(isset($_POST["Submit"])) {
check_form();
} else {
show_form();
}
function validatePhoneNo($telephone) {
if(ereg('^[0-1]{1}[0-8]{1}[0-9]{9}$', $telephone))
{
return true;
}
else
{
return false;
}
}
if ((empty($telephone)) || (!validatePhoneNo($telephone)) || (strlen($telephone)!=11))
{
}
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier.
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;
$error['name'] = false;
if($_POST["name"]=="") {
$error['name'] = true;
$print_again = true;
$message="The name field is empty<br>";
}
if(!validatePhoneNo($_POST['telephone'])) {
$error['telephone'] = true;
$print_again = true;
$message="The telephone field is empty or invalid<br>";
}
if(!check_email_address($_POST['email'])) {
$error['email'] = true;
$print_again = true;
$message.="Required Field Empty or Invalid Email Address <br>";
}
if($print_again) {
show_form();
} else {
$numberofdebts = $_POST['numberofdebts'];
$totalincomepermonth = $_POST['totalincomepermonth'];
$employed = $_POST['employed'];
$location = $_POST['location'];
$name = $_POST['name'];
$housenumber = $_POST['housenumber'];
$postcode = $_POST['postcode'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$totalunsecureddebt = $_POST['totalunsecureddebt'];
$to = "xxx@xxxx.co.uk"; // change to your email address
$from = 'apps@xxxxxxx.co.uk';
$name = $_POST['name'];
$mail = $from;
$subject = "xxxxxxx Application";
$msg = $subject;
$d = date('l dS \of F Y h:i:s A');
$sub = "xxxxxxx Application";
$headers = "From: $name <$from>\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$mes = "Name: ".$name."\n";
$mes .= "Telephone Number: ".$telephone."\n";
$mes .= "House Number: ".$housenumber."\n";
$mes .= "Postcode: ".$postcode."\n";
$mes .= 'Country: '.$location."\n";
$mes .= 'Email: '.$email."\n";
$mes .= "Total Unsecured Debt: ".$totalunsecureddebt."\n";
$mes .= 'Number of debts: '.$numberofdebts."\n";
$mes .= 'Income Per Month: '.$totalincomepermonth."\n";
$mes .= 'Employment Status: '.$employed."\n";
$mes .= 'Date & Time: '.$d;
mail($to, $sub, $mes, $headers);
}
if ( $totalunsecureddebt < 4000 ) {
header( "Location: http://xxx.xxxxxxxxx.co.uk/plan1.php" );
exit();
}
if ( $totalunsecureddebt > 4001 AND $totalunsecureddebt < 14999 ) {
header( "Location: http://xxx.xxxxxxxxxxx.co.uk/plan2.php" );
exit();
}
if ( $totalunsecureddebt > 15000 ) {
header( "Location: http://xxx.xxxxxxxxxx.co.uk/plan3.php" );
exit();
}
echo "$message";
}
?>
I have tried moving the redirect snippet but the same thing always happens, the validation just stops working. When i remove the redirection script the validation works fine, any help would be greatly appreciated.
cheers