dqwh1218 2014-04-09 11:10
浏览 34
已采纳

多字段PHP表单提交

The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:

$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
" . "Name: $name
" . "Phone: $phone
" . "Date: $date
" . "Time: $time
" . "Keep Data? $data
");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }

This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data ). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:

<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
" . "Name: $name
" . "Phone: $phone
" . "Date: $date
" . "Time: $time
" . "Keep Data? $data
");
          echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
          }
        }
    ?>

</body>

and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!

EDIT:

okay so that issue is sorted with:

          $subject = $_POST['inputdropdown'];
          $message = $_POST['inputmessage'] . "

" . $_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";

Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:

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

Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.

EDIT

So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:

<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'] . "

" . "Email: " . $_POST['inputemail'] . "

" . "Phone: " . $_POST['inputphone'] . "

" . "Date: " . $_POST['inputdate'] . "

" . "Time: " . $_POST['inputtime'] . "

" . "Retain Data? " . $_POST['inputprotection'] . "

" . "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>

The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!

FINAL EDIT

My final, fully working, code:

<?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'] . "

" . "Email: " . $_POST['inputemail'] . "

" . "Phone: " . $_POST['inputphone'] . "

" . "Date: " . $_POST['inputdate'] . "

" . "Time: " . $_POST['inputtime'] . "

" . "Retain Data? " . $_POST['inputprotection'] . "

" . "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条回答 默认 最新

  • doulingna9420 2014-04-09 11:18
    关注

    Try putting your form data into the $message variable, insert after each attribute to give a new line. Do not use the header.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题