user2360274
user2360274

Reputation:

HTML form isn't emailing

I have this as my form

        <div class="contactInputs">
                <p>Send us a message</p>
                <form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
                  <input type="text" name="name" placeholder="Name*" required>
                  <input type="text" name="companyname" placeholder="Company Name">
                  <input type="email" name="email" placeholder="Email*" required>
                    <input type="tel" name="phone" placeholder="Phone">
                    <input class="contact-submit" type="submit">
                </form>
                <textarea type="textarea" name="message" placeholder="Your Messages*" required></textarea>
                </div>

And this is the PHP used to do send the Email.

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

$email_to = "[email protected]";
$email_subject = "Your email subject line";


function died($error) { 

    echo "We are very 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();
}


if(!isset($_POST['name']) ||
    !isset($_POST['companyname']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name'];
$companyname = $_POST['companyname'];
$email_from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['comments'];

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$companyname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($message) < 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($name)."\n";
$email_message .= "Last Name: ".clean_string($companyname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "phone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($message)."\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 us. We will be in touch with you very soon.

<?php
}
?>

But whenever i try to submit it, i get the errors

We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

We are sorry, but there appears to be a problem with the form you submitted.

Please go back and fix these errors.

Does anyone see whats wrong

Upvotes: 0

Views: 130

Answers (4)

Enigmadan
Enigmadan

Reputation: 3408

The php if statement is looking for a comments field which you don't have. This makes your if statement true, and kills the php program.

if(!isset($_POST['name']) ||
  !isset($_POST['companyname']) ||
  !isset($_POST['email']) ||
  !isset($_POST['phone']) ||
//It's looking for comments 
  !isset($_POST['comments'])) {
    //Your error message
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

You can solve this by either including a "comments" part in your form, or by removing it entirely from the php so that it won't expect it.

You appear to be trying to accomplish something similar to a "comments" form element with an element called "message", however, this element is outside of the form that is being submitted. If you want to have "message" be part of the form, make sure that it is inside of the <form> tags.

<form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
    <input type="text" name="name" placeholder="Name*" required>
    <input type="text" name="companyname" placeholder="Company Name">
    <input type="email" name="email" placeholder="Email*" required>
    <input type="tel" name="phone" placeholder="Phone">
    <!--New placement of message element-->
    <textarea type="textarea" name="message" placeholder="Your Messages*" required></textarea>
    <input class="contact-submit" type="submit">
</form>

Also, if you keep the name of the "message" element instead of changing it to comment, remember to fix the php code to reflect that change. There are two places in the code that currently refer to a "comment" element:

The one shown above, and also when you define your variables.

$name = $_POST['name'];
$companyname = $_POST['companyname'];
$email_from = $_POST['email'];
$phone = $_POST['phone'];
//refers to comments
$message = $_POST['comments'];

I hope this helps!

Upvotes: 1

GuiGreg
GuiGreg

Reputation: 341

You don't have a 'comments' input in your form.

Upvotes: 1

immulatin
immulatin

Reputation: 2118

 !isset($_POST['comments']))

This field does not exist in your HTML. You have a "message" field so maybe this should be:

 !isset($_POST['message']))

Upvotes: 0

65Fbef05
65Fbef05

Reputation: 4522

You don't seem to have an input field named comments in your form. Also, your textarea is outside of your form, which I presume you meant to name comments. Change your HTML to:

<form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
    <input type="text" name="name" placeholder="Name*" required>
    <input type="text" name="companyname" placeholder="Company Name">
    <input type="email" name="email" placeholder="Email*" required>
    <input type="tel" name="phone" placeholder="Phone">
    <textarea type="textarea" name="comments" placeholder="Your Messages*" required></textarea>
    <input class="contact-submit" type="submit">
</form>

Upvotes: 0

Related Questions