【问题标题】:Why my PHP don´t catch data from HTML form为什么我的 PHP 不能从 HTML 表单中捕获数据
【发布时间】:2021-06-02 09:35:34
【问题描述】:

这是我的代码。我检查了 post.php,这显示为 NULL。在 PHP 中使用 IF 之前,我用 var_dump($_POST['name']); 得到了 Void;并接收空白电子邮件,仅带有 $contenido 的文本

    <?php

var_dump($_POST[]);

  if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['email']) && !empty($_POST['email']) AND !empty($_POST['subject']) AND !empty($_POST['message'])){
    
  $destino="contacto@mail.com";
  $nombre=$_POST["name"];
  $correo=$_POST["email"];
  $asunto=$_POST["subject"];
  $mensaje=$_POST["message"];
  $headers = "From: mi@cuentadeemail.com" . "\r\n" .
  $contenido="Nombre: ".$nombre."\nCorreo: ".$correo."\nAsunto: ".$asunto."\nMensaje: ".$mensaje;
             
  mail($destino, $subject, $contenido, $headers);
  header("Location: index.html");

}

?>
<form id="main-contact-form" name="contact-form" method="post" action="post.php">
                <div class="row  wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="text" id="name" name="name" class="form-control" placeholder="Nombre" required="required">
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="email" id="email" name="email" class="form-control" placeholder="Email" required="required">
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <input type="text" id="subject" name="subject" class="form-control" placeholder="Asunto" required="required">
                </div>
                <div class="form-group">
                  <textarea name="message" id="message" class="form-control" rows="4" placeholder="Mensaje" required="required"></textarea>
                </div>                        
                <div class="form-group">
                  <button type="submit" class="btn-submit">Enviar</button>
                </div>
              </form> 

<!-- begin snippet: js hide: false console: true babel: false -->

【问题讨论】:

  • !empty 包括 isset 免费

标签: php html forms web


【解决方案1】:

尝试更改表单操作

action="post.php" 

到

action="/url/post.php"

或确保您在正确的位置发帖!

【讨论】:

  • 它在同一级别,在我的 web 的根目录中
【解决方案2】:

首先,您不需要required="required" required 标签本身就可以。

正如 Met Byrdy 所说,请确保您发布到正确的位置。如果 post.php 与您的 main-contact-form 位于同一目录中,则 action="post.php" 很好,但如果不是,则需要 action="path/to/post.php"。

【讨论】:

  • 谢谢。我的 post.php 与我的 index.html 位于同一目录中。
  • 取出所有if条件,留下isset($_POST['name']),看看是否有效
猜你喜欢
  • 1970-01-01
  • 2022-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 2022-06-16
  • 1970-01-01
相关资源
最近更新 更多