多字段PHP表单提交

时间:2014-04-09 11:10:46

标签: php html forms email multiple-instances

我遇到的问题是我希望将表单中多个字段的数据传递到一个通过PHP发送的电子邮件中,相关代码如下:

$mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          // This still needs to be debugged online as soon as possible
          $from = $_POST['inputemail'];
          $name = $_POST['inputname'];
          $phone = $_POST['inputphone'];
          $date = $_POST['inputdate'];
          $time = $_POST['inputtime'];
          $data = $_POST['inputprotection'];
          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          mail("UP498701@myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }

这是我宣布将所有信息作为电子邮件标题传递的地方。当前的PHP传递语法检查在发送包含$to$subject$message的电子邮件的范围内工作正常,但$ header仅通过最后一部分(Keep数据?$data\n)。当我删除所有其他字段并只保留$ data部分时,电子邮件将停止发送任何$header。我也遇到了重定向的问题,该问题已从以下代码中删除,并将在当前问题解决后立即插入。当前完整的PHP是:

<html>
<body>

    <?php
    function spamcheck($field)
      {
      // Sanitize e-mail address
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      // Validate e-mail address
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    ?>

    <?php 
    if (isset($_POST['inputemail']))
        {
        $mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          // This still needs to be debugged online as soon as possible
          $from = $_POST['inputemail'];
          $name = $_POST['inputname'];
          $phone = $_POST['inputphone'];
          $date = $_POST['inputdate'];
          $time = $_POST['inputtime'];
          $data = $_POST['inputprotection'];
          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          mail("UP498701@myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }
    ?>

</body>

暂时托管在http://luke-hale.com/temp/contact.html。我确定这只是我的错误信息,但任何帮助都会很棒,欢呼!

编辑:

好的,以便将问题排序为:

          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
          $message = wordwrap($message, 70);
          $headers = "From: " . $_POST['inputemail'];
          mail("UP498701@myport.ac.uk",$subject,$message,$headers);
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";

哪个有效但不会在任何地方显示$ header,但我对此并不太感兴趣,接下来就是重定向,我通常会通过以下方式运行:

header("refresh:5;url=http://www.domain.com")

虽然之前没有正常工作,但我会在测试任何人未来参考时应用编辑。

修改

因此表格有效,但重新指示则不然。该网站仍然托管在同一个域中,但现在完整的PHP看起来像这样:

<html>
<body>

    <?php
    function spamcheck($field)
      {
      // Sanitize e-mail address
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      // Validate e-mail address
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    ?>

    <?php 
    if (isset($_POST['inputemail']))
        {
        $mailcheck = spamcheck($_POST['inputemail']);
        if ($mailcheck==FALSE)
          {
          echo "Invalid input";
          }
        else
          {
          $subject = $_POST['inputdropdown'];
          $message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
          $message = wordwrap($message, 70);
          $headers = "From: " . $_POST['inputemail'];
          mail("UP498701@myport.ac.uk",$subject,$message,$headers);
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }
    header("refresh:5;url=http://www.domain.com")
    ?>

</body>

表单仍然发送正常,但返回表明行开头&#34;标题&#34;无法传递,因为浏览器数据已被修改。我不确定我做了什么,或者在哪里,如果有人可以伸出援助之手,那就太棒了!

最终编辑

我的最终,完全正常的代码:

<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701@myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>

3 个答案:

答案 0 :(得分:1)

尝试将表单数据放入$message变量,在每个属性后插入\n以提供新行。不要使用标题。

答案 1 :(得分:1)

位置和刷新都需要绝对URI

header('Location: http://www.domain.com');

邮件示例:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793

mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");   
你说这个: &#34;邮件功能也不是问题,正确的语法是&#34; mail($to,$subject,$message,$headers,$parameters)

说明:

your email adress = $to  
$subject = $subject  
$therest = $message (rename it if you want)  
"subject: $subject\n" = the header (change it to $headers if you want that)

标题示例(如documentation

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

答案 2 :(得分:0)

您可以在PHP脚本的底部回显一个Javascript,它改为重定向,如下所示:

$url = "url";
$time = 5000 // time in milliseconds

print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.")       </script>"