【问题标题】:My PHP/HTML form doesn't work我的 PHP/HTML 表单不起作用
【发布时间】:2015-05-30 15:27:57
【问题描述】:

我的网站上有一个联系表,但它不能正常工作。我收到了电子邮件,但电子邮件是空的,我没有从表单中获取信息。

我的 index.html 中的 html 部分

<form role="form" form name="contactform" method="post" action="send_form_email.php"> 
                 <div class="form-group">
                        <label>Nombre</label> <input type="text" name="nombre" class="form-control" placeholder="Introduce tu nombre" >
                  </div>
                 <div class="form-group">
                        <label>Apellidos</label> <input type="text" name="apellidos" class="form-control" placeholder="Introduce tus apellidos" >
                  </div>
                 <div class="form-group">
                        <label>Email</label> <input type="emal" name="email" class="form-control" placeholder="Introduce tu email" >
                  </div>
                 <div class="form-group">
                        <label>Telefono</label> <input type="text" name="telefono" class="form-control" placeholder="Introduce tu telefono" >
                  </div>

                  <div class="form-group">
                        <label>Mensaje</label> <textarea name="mensaje" class="form-control" rows="7"></textarea>
                  </div>

                     <button type="submit" class="btn btn-default" value="Submit">   <a href="send_form_email.php">Enviar</a>
                 </form>

                </div>

php 文件中名为“send_form_email.php”的部分

    <?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent=" $nombre $apellidos $email $telefono $mensaje";
$recipient = "deniz946@gmail.com";
$subject = "Subject from $nombre";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>

【问题讨论】:

  • 使用var_dump() 或print_r() 和exit() 来调试你的代码。例如。 var_dump($message); exit();。在您拨打mail() 之前执行此操作,这样您就可以立即解决哪些问题。

标签: php html forms contact


【解决方案1】:

&lt;input type="emal" name="email" class="form-control" placeholder="Introduce tu email" &gt;改成

&lt;input type="email" name="email" class="form-control" placeholder="Introduce tu email" &gt;

type 属性中有错字。

【讨论】:

    【解决方案2】:

    试试这个

    <?php
    $nombre      = $_POST['nombre'];
    $apellidos   = $_POST['apellidos'];
    $telefono    = $_POST['telefono'];
    $email       = $_POST['email'];
    $mensaje     = $_POST['mensaje'];
    $formcontent = "{$nombre} {$apellidos} {$email} {$telefono} {$mensaje}";
    $recipient   = "deniz946@gmail.com";
    $subject     = "Subject from $nombre";
    
    if(PHP_OS == "Linux" || PHP_OS == "Darwin") $new_line = "\n"; /* if Linux or Mac */
    elseif(PHP_OS == "WINNT") $new_line = "\r\n"; /* if Windows */
    $mailheader  = "MIME-Version: 1.1" . $new_line;
    $mailheader .= "Content-type: text/html; charset=utf-8" . $new_line;
    $mailheader .= "From: ".$recipient.$new_line;
    $mailheader .= "Return-Path: " . $recipient . $new_line;
    $mailheader .= "Reply-To: " . $recipient . $new_line;
    
    mail($recipient, $subject, $formcontent, $mailheader, "-r" . $recipient) or die("Error!");
    echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
    ?>
    

    【讨论】:

    • 现在对我有用!!!非常感谢你,你能解释一下你添加的邮件头是什么吗? :)
    • @FridoxFL - Charset 和 From 是不言自明的.... Return-Path 和 Reply-To 用于电子邮件回复。您可以使用 Content-type text 或 text/html 使消息更漂亮。
    【解决方案3】:

    也许我可以用一个我自己使用的例子来帮助你:

    HTML 联系表:

                <div class="row">
    
                    <!-- Content -->
                        <div id="content" class="8u skel-cell-mainContent">
                            <section class="12u">
    
                                <header>
                                    <h2>Contact</h2>
                                </header>
    
                             <form method="post" action="mail.php">
                            <div class="row half">
                                <div class="6u">
                                    <input name="subject" placeholder="Name" type="text" class="text" />
                                </div>
                                <div class="6u">
                                    <input name='email' placeholder="Email" type="text" class="text" />
                                </div>
                            </div>
                            <div class="row half">
                                <div class="12u">
                                    <textarea name="message" placeholder="Message"></textarea>
                                </div>
                            </div>
                            <div class="row half">
                                <div class="12u">
                                    <!--a href="mail.php" class="button">Submit</a-->
                                    <input type="submit" class="button" value="Submit">
                                </div>
                            </div>
                        </form>
    
                            </section>
    
                        </div>
    

    表单的PHP处理:

    <?php 
                    //mail(to,subject,message,headers,parameters)
    
    
                        // Check if the "from" input field is filled out
                        //if (isset($_POST['from'])) 
                        //{}
                          $to = 'mail@xxxxxx.com'; // my email address
                          $subject = $_POST['subject']; //name client
    
                          $feedback = 'thank you, we will reply soon.';
                          $header  = $_POST['email']; //Email client
    
                          $message = $_POST['message'];
                          $message = wordwrap($message, 70);
    
                          $message = <<<EMAIL
    Dear (YOUR WEBSITE NAME HERE),
    
    My name is: $subject
    
    $message
    
    Email Client: $header
    
    
    EMAIL;
    
    
    
    
    
                          // send mail
                          if($_POST){ 
                          mail($to, $subject, $message, $header);
    
                          echo $feedback;
                                    }
    
    
    
    
    
    ?>
    

    请仔细记住,从 Dear... 到 EMAIL 的部分必须靠在屏幕左侧,不能有任何制表符或空格。否则就不行了!

    希望你能用它来取得成功:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2011-12-30
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多