1

I have designed a form in html and I'm using php to send it to my email. The form validates OK and everything is fine apart from the fact that it doesn't send some information. Here is how it comes to my email:

Name: 
Email: 
Tel.: 
Message: test

So when I fill it in the only field that goes through is the "Message" field. The other ones are blank although the form validates and process. I guess there is something in php code. I don't know much about php, so need some help please. Thanks.

          <form action="post.php" method="post">
                <div class="form-group">
                  <label for="name">Name *</label>
                  <input type="text" class="form-control" id="name"
                  placeholder="" required="required" oninvalid="this.setCustomValidity('Please enter your name')"
                  oninput="setCustomValidity('')">
                </div>
                <div class="form-group">
                  <label for="email">E-mail *</label>
                  <div class="input-group">
                    <span class="input-group-addon">
                      <span class="glyphicon glyphicon-envelope"></span>
                    </span>
                    <input type="email" class="form-control" id="email" placeholder="" required="required"
                    oninvalid="this.setCustomValidity('please enter your email')" oninput="setCustomValidity('')">
                  </div>
                </div>
                <div class="form-group">
                  <label for="name">phone *</label>
                  <input type="text" class="form-control" id="phone"
                  placeholder="" required="required" oninvalid="this.setCustomValidity('please enter your phone')"
                  oninput="setCustomValidity('')">
                </div>
              </div>
              <div class="col-md-12">
                <div class="form-group">
                  <label for="name">message *</label>
                  <textarea name="message" id="message" class="form-control"
                  rows="10" cols="25" required="required" placeholder="" oninvalid="this.setCustomValidity('please enter your message')"
                  oninput="setCustomValidity('')"></textarea>
                </div>
              </div>
              <div class="col-md-12">
                <button type="submit" class="btn btn-primary pull-right" id="btnContactUs">send</button>

          </form>

and php code:

<?php

$addressto = "info@xxx.com";
$subject = "Message from the website";
$content = "Name: ".$_POST['name']."\n"
           ."Email: ".$_POST['email']."\n"
           ."Tel.: ".$_POST['phone']."\n"     
           ."Message: ".$_POST['message']."\n";

if(!$_POST['name'] || !$_POST['email'] || !$_POST['message']){
    header("Location: contact.html");
}

$email = $_POST['email'];
if (mail($addressto, $subject, $content, 'Od: <' . $email . '>')) {
    header("Location: contact-ok.html");
}

?>
Piotr Ciszewski
  • 1,691
  • 4
  • 30
  • 53

1 Answers1

3

Your input fields are missing the name attribute

id is mostly for JS/CSS manipulation, while name will be the keys in your POST array.

They can both be the same. Hence why your message works.

Remember that JS validation can easily be bypassed and that the user input should also be checked in php.

<input type="text" class="form-control" id="name" name="name"...>
hexYeah
  • 1,040
  • 2
  • 14
  • 24