【问题标题】:PHP code not connecting with html [duplicate]PHP代码未与html连接[重复]
【发布时间】:2017-06-16 20:39:30
【问题描述】:

我很难让我的 PHP 和 HTML 在电子邮件表单上协同工作。这是我采用的一个网站,我不确定我错过了什么。我基本上是在模式中构建一个表单,因此用户必须在下载资产之前注册他们的电子邮件。因此,一旦发送了表单,他们就可以连接到下一页以下载他们需要的内容。我遇到的麻烦是表格。无论我尝试什么都行不通。救命!

 <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Download 3D Models</h4>
      </div>
      <div class="modal-body">
       <div class="container slate">
      <div class="row">
        <div class="col-md-6">
        <form name="send_form_email" action="../quform/send_form_email.php" method="get" enctype="multipart/form-data">
        <div class="quform-elements">
         <div class="form-group">
                <label for="email">Name <span class="text-danger">*</span></label> 
                <input id="name" type="text" name="name" class="form-control" placeholder="Name">
              </div>
              <div class="form-group">
                <label for="company">Company / Firm <span class="text-danger">*</span></label>
                <input id="company" type="text" name="email" class="form-control" placeholder="Company / Firm">
              </div>
              <div class="form-group">
                <label for="email">Email<span class="text-danger">*</span></label>
                <input id="email" type="text" name="email" class="form-control" placeholder="Email">
              </div>
             <div class="quform-element quform-element-recaptcha">
                <div class="quform-spacer">
                  <label>Are you human?<span class="text-danger">*</span></label>
                  <div class="quform-input" style="width: 250px;">
                    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
                    <div class="g-recaptcha" data-sitekey="6Le2WvkSAAAAAP9bLeD4OaiVYvDrHXhcKaFo1chy"></div>
                    <noscript>
                      <div style="width: 302px; height: 352px;">
                        <div style="width: 302px; height: 352px; position: relative;">
                          <div style="width: 302px; height: 352px; position: absolute;">
                            <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Le2WvkSAAAAAP9bLeD4OaiVYvDrHXhcKaFo1chy" frameborder="0" scrolling="no" style="width: 302px; height:352px; border-style: none;">
                            </iframe>
                          </div>
                          <div style="width: 250px; height: 80px; position: absolute; border-style: none; bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 5px;">
                            <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 80px; border: 1px solid #c1c1c1; margin: 0px; padding: 0px; resize: none;"></textarea>
                         </div>
                        </div>
                      </div>
                    </noscript>
                  </div>
                </div>
              </div><!--/.recaptcha -->
              <div class="form-group">
                <button type="reset" class="btn btn-default">Cancel</button>
                <button type="submit" class="btn btn-primary" value="Send">Send</button>
              </div>
            </div> <!--/. quform-elements-->
          </form>      
        </div> <!--/.col-md-6-->

<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$company = $_POST['company'];
$email = $_POST['email'];
$human = $_POST['human'];
$from = $_POST['email'];
$to = 'xxx@gmail.com'; //set to the default email address
$subject = $_POST['3D Model Request'];
$body = "From: $name\n Company: $company/n E-Mail: $email\n";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

if(isset($_POST['submit']) && ($_POST['human']) == '4') {               
mail ($to, $subject, $body, $headers);  //mail sends it to the SMTP server side which sends the email
    echo "<p>Your message has been sent!</p>";
} 

else { 
    echo "<p>Something went wrong, go back and try again!</p>"; 
} 
if (!isset($_POST['submit']) && ($_POST['human']) != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>

【问题讨论】:

  • 会不会是邮件发到$to = 'xxx@gmail.com';
  • 您在表单中发送获取数据,在等待php中的POST时,您应该将method="get"更改为method="post"
  • 我只是将它作为占位符放入以保护我的电子邮件。使用我的真实电子邮件没有帮助。
  • “无论我尝试什么都行不通。”你期望的行为是什么,你得到的行为是什么?
  • 我希望将电子邮件发送给我自己。单击“提交”后,我目前正在获得一个带有代码的空白页面。我正在使用我的 MAMP 测试服务器,如果有帮助的话...

标签: php html forms email simplemodal


【解决方案1】:

您的表单有method="get",并且您的 php 逻辑期待 POST 数据,因为您是通过 get 提交的,所以该数据永远不会设置。

将表单更改为 method="POST"

&lt;form name="send_form_email" action="../quform/send_form_email.php" method="POST" enctype="multipart/form-data"&gt;

【讨论】:

  • 我也试过了,还是不行。还有其他想法吗?
  • 在您的 PHP 文件顶部执行 var_dump($_POST) 并查看数据是否正在提交到您的页面进行处理。
  • 已添加。在我的表单上点击“提交”后,它给了我一个带有代码的空白页。所以有些事情处理不正确。
  • @JillPut 如果它为您提供了带有 php 代码的空白页面,您应该检查您的网络服务器设置以启用 php
【解决方案2】:

您的表单在 get 中发送数据,但在 php 端您正在等待从 POST 获取数据,更改表单的 method 以使其工作。 应该是method="POST" 此外,您还有多个 email 输入(它们具有相同的名称)。 而且您没有收到来自此表单的$human = $_POST['human'];$subject = $_POST['3D Model Request'];

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 2022-01-21
    • 2017-01-19
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多