douka19950505
douka19950505
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:

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']."
"
           ."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");
}

?>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

相关推荐