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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)