1

I'm trying to create a simple form that when filled out sends me a specific email. The website definitely registers the click and tells me that my message was sent, but no email ever comes.

If I, however, get rid of the if-statement, it does send me an email, but only when I refresh the page, so it doesn't send me any of the relevant information. So, my if-statement is wrong, but when I change it to strlen($name)!=0, it still doesn't send me the email.

I think the problem is that when I click the button, it doesn't run the php code. How do I fix this? Thanks.

Here is my code:

<form method="post" id="contactForm" name="sentMessage" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
            <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="col-lg-12 text-center">
        <div id="success"></div>
        <button type="submit" id="submit" class="btn btn-xl">Send Message</button>
    </div>
</div>
 <?php
    $name=$_POST["name"];
    $email=$_POST["email"];
    $phone=$_POST["phone"];
    $message=$_POST["message"];
    if(isset($_POST["submit"])){
        $message=$message."\nSent by".$name." whose phone number is ".$phone."and whose email is".$email;
        mail("myemail@gmail.com","title","let's try this message.".$message);
    }
?>

Jimmy Kim
  • 93
  • 1
  • 8

1 Answers1

3

The $_POST data elements use the name attribute, not the id attribute; change all the ids in the form to name.

For example:

<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">

should be:

<input type="tel" class="form-control" placeholder="Your Phone *" name="phone" required data-validation-required-message="Please enter your phone number.">
                                                                  ^^^^

Now, the $_POST["phone"] should get the right value. Do this for all of the other values.

See this SO answer for more details.

Community
  • 1
  • 1
Jonathan Lam
  • 15,294
  • 14
  • 60
  • 85