2 replies
I'm trying to add a contact form to one of my sites and I keep getting a page full of errors on submission (all echos show up). Can anyone tell me what I'm doing wrong? My php file is saved with the correct name. Thanks.

HTML:

<form name="contactform" method="post" action="send_form_mail.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name"><font color="#ffffff">First Name *</font></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>

<tr>
<td valign="top"">
<label for="last_name"><font color="#ffffff">Last Name *</font></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><font color="#ffffff">Email Address *</font></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>

</tr>
<tr>
<td valign="top">
<label for="telephone"><font color="#ffffff">Telephone Number</font></label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><font color="#ffffff">Question/Comment *</font></label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>

</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>

---------------------

PHP:

<?php
if(isset($_POST['email'])) {

$email_to = "matt@gylbo.com";
$email_subject = "GYLBO Inquiry";


function died($error) {

echo "I'm sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('I'm sorry, but there appears to be a problem with the form you submitted.');
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!eregi($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>


Thank you for contacting me. If you require a response I should be getting back to you within the next 48 hours.

<?php
}
?>
#contact #errors #form
  • Profile picture of the author webpro4hire
    Hi Matt,

    hard to tell what causes the errors without seeing what you are submitting.
    From looking at the code, nothing other than the various tests can trigger the errors.

    Perhaps you have spaces at the end of first name or last name?

    i would add a "trim" to each input variable, to remove superfluous spaces at the beginning and end of each string.

    Good luck,
    WebPro
    {{ DiscussionBoard.errors[3355264].message }}
    • Profile picture of the author Tim Brownlaw
      G'day Matt,

      Here's a handy tip for you....
      When coding, use something ( I use a text editor ) that has syntax highlighting.
      It's a handy way to see when these kind of things go whoopsie!

      I recreated your two files and in my editor, things went mad ( visually ) on line 23 of your send_form_mail.php

      PHP Code:
      died('I'm sorrybut there appears to be a problem with the form you submitted.');

      Spot the problem? You have enclosed your string with single quotes and tossed in an apostrophe (single quote) which upsets PHP somewhat.
      See the Syntax highlighter in the snippet above got confused.

      You have two choices.
      1. instead of enclosing your string with single quotes - use double quotes.
      PHP Code:
      died("I'm sorry, but there appears to be a problem with the form you submitted.");

      2. or escape the offending single quote so the PHP parser ignores it.
      PHP Code:
      died('I\'m sorry, but there appears to be a problem with the form you submitted.');

      Which is better?
      The simple answer...
      Which ever works for you! Just remember, what's on the outside cannot be on the inside. If they are - swap them or escape them.


      Good to see you've got filtering on your email addys too!
      {{ DiscussionBoard.errors[3366302].message }}

Trending Topics