PHP Validation failing

I an having problems validating a form serverside with php, my code is not validating and I traced it to this section of the form but still cant figure out whats wrong.

HTML CODE

<input type="text" name="ReferredsFirstName" id="ReferredsFirstName"/>

PHP CODE

$EmailTo = "my@emailaddress.net";
$EmailFrom = "my@emailaddress.net";
$Subject = "Referral Form";
$ReferredsFirstName = Trim(stripslashes($_POST['ReferredsFirstName']));

$validationOK=true;
if (Trim($ReferredsFirstName)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-error.html\\">";
exit;
}

$Body .= "\
";
$Body .= "Reffereds First Name: ";
$Body .= $ReferredsFirstName;
$Body .= "\
\
";

$genuine_data = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($genuine_data){
print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-success.html\\">";
}

else{
print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-error.html\\">";
}

Anyone have any ideas?

If I comment out the RefferedsFirstName in the validation block of code it works fine.

At first glance, I’d say it’s because Trim is capitalized.

Thanks for having a look Zurev

I dont think this is the issue though because the rest of my form has the same casing.

And as mentioned in my opening post if I comment out this section of code

// if (Trim($ReferredsFirstName)=="") $validationOK=false;

it validates and sends email fine

ok, you just need to do some basic debugging

  1. start at the top of the script as specified in your form’s action attribute and add
 
echo 'got here'; die();

  1. run your form to check if it gets to your php script

  2. then move the above echo/die down, line by line if you have to, and add appropriate echo statements to display values of variables and then run the form again each time you move the echos.

  3. as part of 3) insert the echos in each part of conditional blocks (IF blocks) to check your code logic is correct

keep doing this until your echos show something is not right. then back track your code to fix the error.

  1. keep repeating 3) and 4) until you get to the end of your script and it works ok.

if you have a debugger, then debugguing will be easier as you can set break points and check values of variables which is essentially what the above steps are doing.

Ok I’ll try that just using the snippets of code I posted here and see how I go

Well I just tried the code snippet I posted on its own and it works fine, I dont have a php debugger so if someone that does could run it through theirs I would much appreciate it.

This is all of the code, I am pretty sure the html is fine but I will post it as well as the php

<form action="/referral.php" method="post" id="ReferForm">
<table width="900" border="0" cellspacing="0" cellpadding="5">
            <tr>
              <td>Title*</td>
              <td><select name="Title" id="Title">
                  <option value="" selected>Please Select</option>
                  <option value="Mr">Mr</option>
                  <option value="Mrs">Mrs</option>
                  <option value="Ms">Ms</option>
                </select></td>
            </tr>
            <tr>
              <td>Your First Name*</td>
              <td><input type="text" name="YourFirstName" id="YourFirstName"/></td>
            </tr>
            <tr>
              <td width="256">Your Last Name*</td>
              <td width="624"><input type="text" name="YourLastName" id="YourLastName"/></td>
            </tr>
            <tr>
              <td>Your Relationship to Person Referred?*</td>
              <td><select name="RelationshipToReferred" id="RelationshipToReferred">
                  <option value="" selected>Please Select</option>
                  <option value="Friend">Friend</option>
                  <option value="Family Member">Family Member</option>
                  <option value="Work Colleague">Work Colleague</option>
                  <option value="Business Associate">Business Associate</option>
                </select></td>
            </tr>
            <tr>
              <td>Your Email Address*</td>
              <td><input type="text" name="YourEmail" id="YourEmail"/></td>
            </tr>
            <tr>
              <td>Your Phone Number*</td>
              <td><input type="text" name="YourPhone" id="YourPhone"/></td>
            </tr>
            <tr>
              <td>Type of product Referred?*</td>
              <td><select name="TypeOfProduct" id="TypeOfProduct">
                  <option value="" selected="selected">Please Select</option>
                  <option value="Financial Planning">Financial Planning</option>
                  <option value="Investment Planning">Investment Planning</option>
                  <option value="Life and Disability">Life and Disability</option>
                  <option value="Share Trading">Share Trading </option>
                </select></td>
            </tr>
            <tr>
              <td>Referred Clients Title</td>
              <td><select name="ReferredsTitle" id="ReferredsTitle">
                  <option value="" selected>Please Select</option>
                  <option value="Mr">Mr</option>
                  <option value="Mrs">Mrs</option>
                  <option value="Ms">Ms</option>
                </select></td>
            </tr>
            <tr>
              <td>Referred Client First Name*</td>
              <td><input type="text" name="ReferredsFirstName" id="ReferredsFirstName"/></td>
            </tr>
            <tr>
              <td>Referred Clients Last Name*</td>
              <td><input type="text" name="ReferredsLastName" id="ReferredsLastName"/></td>
            </tr>
            <tr>
              <td>Referred Clients Email</td>
              <td><input type="text" name="ReferredsEmail" id="ReferredsEmail"/></td>
            </tr>
            <tr>
              <td>Referred Clients Work Phone Number</td>
              <td><input type="text" name="ReferredsWorkPhone" id="ReferredsWorkPhone"/></td>
            </tr>
            <tr>
              <td>Referred Clients Home Phone Number</td>
              <td><input type="text" name="ReferredsHomePhone" id="ReferredsHomePhone"/></td>
            </tr>
            <tr>
              <td>Referred Clients Mobile Number*</td>
              <td><input type="text" name="ReferredsMobilePhone" id="ReferredsMobilePhone"/></td>
            </tr>
            <tr>
              <td>Best time to call Referred Client?*</td>
              <td><select name="TimeToCallRefferred" id="TimeToCallRefferred">
                  <option value="" selected="selected">Please Select</option>
                  <option value="Morning Business Hours">Morning Business Hours</option>
                  <option value="Between 12 and 1pm">Between 12 and 1pm</option>
                  <option value="Afternoon Business Hours">Afternoon Business Hours</option>
                </select></td>
            </tr>
            <tr>
              <td>Comments</td>
              <td rowspan="2"><textarea name="Comments" id="Comments" rows="3"></textarea></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input type="submit" name="Submit" value="Submit" /></td>
            </tr>
          </table>
</form>
<?php		 
		$EmailTo = "my@emailaddress.net";
		$EmailFrom = "my@emailaddress.net";
		$Subject = "Referral Form";
		$Title = Trim(stripslashes($_POST['Title']));
		$YourFirstName = Trim(stripslashes($_POST['YourFirstName']));
		$YourLastName = Trim(stripslashes($_POST['YourLastName']));
		$RelationshipToReferred = Trim(stripslashes($_POST['RelationshipToReferred']));
		$YourEmail = Trim(stripslashes($_POST['YourEmail']));
		$YourPhone = Trim(stripslashes($_POST['YourPhone']));
		$TypeOfProduct = Trim(stripslashes($_POST['TypeOfProduct']));
		$ReferredsTitle = Trim(stripslashes($_POST['ReferredsTitle']));
		$ReferredsFirstName = Trim(stripslashes($_POST['ReferredsFirstName']));
		$ReferredsLastName = Trim(stripslashes($_POST['ReferredsLastName']));
		$ReferredsEmail = Trim(stripslashes($_POST['ReferredsEmail']));
		$ReferredsWorkPhone = Trim(stripslashes($_POST['ReferredsWorkPhone']));
		$ReferredsHomePhone = Trim(stripslashes($_POST['ReferredsHomePhone']));
		$ReferredsMobilePhone = Trim(stripslashes($_POST['ReferredsMobilePhone']));
		$TimeToCallRefferred = Trim(stripslashes($_POST['TimeToCallRefferred']));
		$Comments = Trim(stripslashes($_POST['Comments']));
		
		

// 		Form validation using a boolean operator
		$validationOK=true;
		if (Trim($Title)=="") $validationOK=false;
		if (Trim($YourFirstName)=="") $validationOK=false;
		if (Trim($YourLastName)=="") $validationOK=false;
		if (Trim($RelationshipToReferred)=="") $validationOK=false;
		if (Trim($YourEmail)=="") $validationOK=false;
		if (Trim($YourPhone)=="") $validationOK=false;
		if (Trim($TypeOfProduct)=="") $validationOK=false;
		if (Trim($ReferredsTitle)=="") $validationOK=false;
		if (Trim($ReferredsFirstName)=="") $validationOK=false;	//problem eliminated if this line is commented out
		if (Trim($ReferredsLastName)=="") $validationOK=false;
		if (Trim($ReferredsMobilePhone)=="") $validationOK=false;
		if (Trim($TimeToCallRefferred)=="") $validationOK=false;
		if (!$validationOK) {
		  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-error.html\\">";
		  exit;
		}

// 		Format the body before emailing form data
		$Body = "";
		$Body .= "Your Title: ";
		$Body .= $Title;
		$Body .= "\
\
";
		$Body .= "Your First Name: ";
		$Body .= $YourFirstName;
		$Body .= "\
\
";
		$Body .= "Your Last Name: ";
		$Body .= $YourLastName;
		$Body .= "\
\
";
		$Body .= "Your releationship to refferred: ";
		$Body .= $RelationshipToReferred;
		$Body .= "\
\
";
		$Body .= "Your Email: ";
		$Body .= $YourEmail;
		$Body .= "\
\
";
		$Body .= "Your Phone Number: ";
		$Body .= $YourPhone;
		$Body .= "\
\
";
		$Body .= "Type of Product Reffered: ";
		$Body .= $TypeOfProduct;
		$Body .= "\
\
";
		$Body .= "Reffereds Title: ";
		$Body .= $ReferredsTitle;
		$Body .= "\
\
";
		$Body .= "Reffereds First Name: ";
		$Body .= $ReferredsFirstName;
		$Body .= "\
\
";
		$Body .= "Reffereds Last Name: ";
		$Body .= $ReferredsLastName;
		$Body .= "\
\
";
		$Body .= "Reffereds Email Address: ";
		$Body .= $ReferredsEmail;
		$Body .= "\
\
";
		$Body .= "Reffereds Work Phone Number: ";
		$Body .= $ReferredsWorkPhone;
		$Body .= "\
\
";
		$Body .= "Reffereds Home Phone Number: ";
		$Body .= $ReferredsHomePhone;
		$Body .= "\
\
";
		$Body .= "Reffereds Mobile Phone Number: ";
		$Body .= $ReferredsMobilePhone;
		$Body .= "\
\
";
		$Body .= "Best time to call Reffered: ";
		$Body .= $TimeToCallRefferred;
		$Body .= "\
\
";
		$Body .= "Comments: ";
		$Body .= $Comments;
		$Body .= "\
\
";
		$Body .= "\
";

// 		send email 
		$genuine_data = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to validator page 
if ($genuine_data){
  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-success.html\\">";
}
else{
  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contact-error.html\\">";
}
?>

then what is the problem and/or what isn’t happening that should happen.

I cannot see the problem, however you said:

If I comment out the RefferedsFirstName in the validation block of code it works fine.

You spelt referreds in two different ways already in your original post, I’d delete it and type it again - or just make it simpler so you are less likely to make a typo such as that.

R_LastName ?

and rather than me running your code through a debugger, why can’t you go through the debugging process I suggested earlier instead and find the error for yourself?

correct me if I’m wrong but it sounds like you want someone to do the debugging for you.

I just rebuilt the php from scratch and it now works fine, I dont think I would have ever found the problem quickly without a debugger.