在Fancybox中提交表单的iframe不发布数据

时间:2015-07-29 20:50:53

标签: php jquery forms iframe fancybox

我正在尝试在fancybox中提交表单。这是一个简单的联系我们表格,名称和电子邮件和提交按钮。 我将fancybox设置为iframe并加载“download.php”。这就是“download.php”的含义:

    <form action="sendmail.php" type="post">
                <fieldset style="width: 300px;">
            <legend>Please fill out completely:</legend>
                <span style="color: red;">* required field</span> 
                <br>
                <label for="txtName">Name:</label><br>
                <input type="text" id="txtName" style="width: 200px;" name="txtName"><span id="lblName" style="color: red;">*</span><br>
                <label for="txtEmail">Email:</label><br>
                <input type="text" id="txtEmail" style="width: 200px;" name="txtEmail"><span style="color: red;">*</span><br><br>
                <button id="btnSubmit">Submit</button>
                <input type="text" id="txtH" style="display: none;" name="txtH">
            </fieldset>
    </form>

提交时,除“txtName”和“txtEmail”为空之外,它的工作正常。我似乎无法获得要发送的值。它在正确提交后发送电子邮件,否则这就是我需要使用PHP而不是jQuery的原因。谢谢你的帮助!

这是sendmail.php代码。

ini_set('SMTP','mail.######.com' ); 
ini_set('smtp_port', '25');
ini_set('sendmail_from', 'info@#####.com');

  $name = $_POST["txtName"];
  $email = $_POST["txtEmail"];
  $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  $msgBody =  'The following information was submitted via the download form.' . PHP_EOL . PHP_EOL . 
  'Name: ' . $name . PHP_EOL .
  'Email: ' . $email . PHP_EOL .
  'URL: ' . $url . PHP_EOL;


  $to = "me@me.com";
  $subject = "A White Paper was downloaded";

  $headers = "From: info@me.com" . "\r\n";

  if (mail($to, $subject, $msgBody, $headers)) {
    header('Location: download-success.php');
  }
  else {
    header('Location: same.php');
  }

}

1 个答案:

答案 0 :(得分:1)

你在这里犯了错误,

<form action="sendmail.php" type="post">

它应该是method="post"而不是type="post"

<form action="sendmail.php" method="post">

并在提交按钮中添加type="submit"

<button id="btnSubmit" type="submit">Submit</button>

如果你想隐藏输入,你不需要设置display:none;你可以使用type="hidden"

<input type="hidden" id="txtH" name="txtH">

并在您的PHP中,使用isset

if (isset($_POST['txtName']) {
    $name = $_POST["txtName"];
    //and rest of the code goes here
}

始终

将错误报告添加到PHP文件的顶部,这有助于查找错误。

error_reporting(E_ALL); //Error reporting enable
ini_set('display_errors',1);