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 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"...>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表
  • ¥15 DbVisualizer Pro 12.0.7 sql commander光标错位 显示位置与实际不符
  • ¥15 android 打包报错