2018-01-19 00:45
浏览 36


This question already has an answer here:

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:

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')"
                <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>
                    <input type="email" class="form-control" id="email" placeholder="" required="required"
                    oninvalid="this.setCustomValidity('please enter your email')" oninput="setCustomValidity('')">
                <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')"
              <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')"
              <div class="col-md-12">
                <button type="submit" class="btn btn-primary pull-right" id="btnContactUs">send</button>


and php code:


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

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");

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongque1646
    dongque1646 2018-01-19 01:03

    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"...>
    点赞 评论