Jump to content

I cant get this PHP code for contact form to work


slehmann36

Recommended Posts

Hi, i am new to the PHP scene and am having trouble getting this PHP code i found to work. I found it here:http://www.inmotionhosting.com/support/edu/website-design/using-php-and-mysql/how-to-create-a-custom-php-contact-form

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title></head> <body><?php if (isset($_REQUEST['submitted'])) {// Initialize error array.  $errors = array();  // Check for a proper First name  if (!empty($_REQUEST['firstname'])) {  $firstname = $_REQUEST['firstname'];  $pattern = "/^[a-zA-Z0-9_]{2,20}/";// This is a regular expression that checks if the name is valid characters  if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}  } else {$errors[] = 'You forgot to enter your First Name.';}    // Check for a proper Last name  if (!empty($_REQUEST['lastname'])) {  $lastname = $_REQUEST['lastname'];  $pattern = "/^[a-zA-Z0-9_]{2,20}/";// This is a regular expression that checks if the name is valid characters  if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}  } else {$errors[] = 'You forgot to enter your Last Name.';}    //Check for a valid phone number  if (!empty($_REQUEST['phone'])) {  $phone = $_REQUEST['phone'];  $pattern = "/^[0-9_]{7,20}/";  if (preg_match($pattern,$phone)){ $phone = $_REQUEST['phone'];}  else{ $errors[] = 'Your Phone number can only be numbers.';}  } else {$errors[] = 'You forgot to enter your Phone number.';}    if (!empty($_REQUEST['redmapleacer']) || !empty($_REQUEST['chinesepistache']) || !empty($_REQUEST['raywoodash'])) {  $check1 = $_REQUEST['redmapleacer'];  if (empty($check1)){$check1 = 'Unchecked';}else{$check1 = 'Checked';}  $check2 = $_REQUEST['chinesepistache'];  if (empty($check2)){$check2 = 'Unchecked';}else{$check2 = 'Checked';}  $check3 = $_REQUEST['raywoodash'];  if (empty($check3)){$check3 = 'Unchecked';}else{$check3 = 'Checked';}  } else {$errors[] = 'You forgot to enter your Phone number.';}  }  //End of validation   if (empty($errors)) {   $from = "From: Our Site!"; //Site name  // Change this to your email address you want to form sent to  $to = "your@email.com";   $subject = "Admin - Our Site! Comment from " . $name . "";    $message = "Message from " . $firstname . " " . $lastname . "   Phone: " . $phone . "   Red Maple Acer: " . $check1 ."  Chinese Pistache: " . $check2 ."  Raywood Ash: " . $check3 ."";  mail($to,$subject,$message,$from);  }?> <!--Error Reporting Code--><?php   //Print Errors  if (isset($_REQUEST['submitted'])) {  // Print any error messages.   if (!empty($errors)) {   echo '<hr /><h3>The following occurred:</h3><ul>';   // Print each error.   foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}  echo '</ul><h3>Your mail could not be sent due to input errors.</h3><hr />';}   else{echo '<hr /><h3 align="center">Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>';   echo "Message from " . $firstname . " " . $lastname . " <br />Phone: ".$phone." <br />";  echo "<br />Red Maple Acer: " . $check3 . "";  echo "<br />Chinese Pistache: " . $check2 . "";  echo "<br />Raywood Ash: " . $check3 . "";  }  }//End of errors array  ?>    <h2>Contact us</h2>  <p>Fill out the form below.</p>  <form action="" method="post">  <label>First Name: <br />  <input name="firstname" type="text" value="- Enter First Name -" /><br /></label>  <label>Last Name: <br />  <input name="lastname" type="text" value="- Enter Last Name -" /><br /></label>  <label>Phone Number: <br />  <input name="phone" type="text" value="- Enter Phone Number -" /><br /></label>  <label>Red Maple Acer:   <input name="redmapleacer" type="checkbox" value="Red Maple Acer" /><br /></label>  <label>Chinese Pistache:   <input name="chinesepistache" type="checkbox" value="Chinese Pistache" /><br /></label>  <label>Raywood Ash:   <input name="raywoodash" type="checkbox" value="Raywood Ash" /><br /></label>  <input name="" type="reset" value="Reset Form" /><input name="submitted" type="submit" value="Submit" />  </form>  </body></html>

I get an issue with the phone number check

 

could someone give me an idea of whats wrong? Thanks Simon Lehmann

Link to comment
Share on other sites

The link provided did not work so I could not check the source code.
A better pattern match is
$pattern = "/^(+d{1,2}s)?(?d{3})?[s.-]d{3}[s.-]d{4}$/";
The if statement has two errors.
1) a elseif is needed
2) a condition/check is needed for the elseif
There are two else in a row; this is not allowed.
it looks like the proper code should be just a single else like this...
  //Check for a valid phone number  if (!empty($_REQUEST['phone'])) {  // something was sent    $phone = $_REQUEST['phone'];    $pattern = "/^[0-9_]{7,20}/";    if (preg_match($pattern,$phone)){       $phone = $_REQUEST['phone'];    } else {   // illegal chars      $errors[] = 'Your Phone number can only be numbers.';    } // end of preg_match  } else {   // no entry - blank    $errors[] = 'You forgot to enter your Phone number.';  } // end of !emptyThis is how the code would be if more than one condition needed to be checked.// proper form of "if...elseif...else"if ( condition ) {// Type your code here} elseif ( condition ) {  // other code} else {  // other code}  //Check for a valid phone number  if (!empty($_REQUEST['phone'])) {    $phone = $_REQUEST['phone'];    $pattern = "/^[0-9_]{7,20}/";    if (preg_match($pattern,$phone)){       $phone = $_REQUEST['phone'];    } else {       $errors[] = 'Your Phone number can only be numbers.';  }  else {       $errors[] = 'You forgot to enter your Phone number.';    }

 

Edited by jwzumwalt
Link to comment
Share on other sites

What does that mean? What actually happens? What are you trying to change?

The code falsely returns the 'You forgot to enter your Phone number.' error when I do enter a phone number, and sometimes the 'Your Phone number can only be numbers.' error when i did only enter numbers.

 

Thanks

Link to comment
Share on other sites

There are two else in a row; this is not allowed.

That's just a lack of indenting on the code, the second else goes with the call to empty above, not with the regular expression match.

The code falsely returns the 'You forgot to enter your Phone number.' error when I do enter a phone number

That's probably coming from this section, which has nothing to do with a phone number:
  if (!empty($_REQUEST['redmapleacer']) || !empty($_REQUEST['chinesepistache']) || !empty($_REQUEST['raywoodash'])) {  $check1 = $_REQUEST['redmapleacer'];  if (empty($check1)){$check1 = 'Unchecked';}else{$check1 = 'Checked';}  $check2 = $_REQUEST['chinesepistache'];  if (empty($check2)){$check2 = 'Unchecked';}else{$check2 = 'Checked';}  $check3 = $_REQUEST['raywoodash'];  if (empty($check3)){$check3 = 'Unchecked';}else{$check3 = 'Checked';}  } else {$errors[] = 'You forgot to enter your Phone number.';}  }

and sometimes the 'Your Phone number can only be numbers.' error when i did only enter numbers.

The regular expression you're using checks for things that are between 7 and 20 characters, and only containing digits or the underscore character. If you enter anything else, including spaces, or if it is too short or too long then it will show that error message.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...