0

我已经在 PHP 中设置了一个电子邮件 Web 表单。表单正常工作并发送电子邮件。但不是重定向它会出现这个错误:错误;您需要提交表格!警告:无法修改标头信息 - 第 70 行 /home/con17120/public_html/form-to-email.php 中的标头已由(输出开始于 /home/con17120/public_html/form-to-email.php:5)发送“虽然表单仍然可以正常工作,但它不会重定向。

.....谢谢HTML:

     <form method="post" name="myemailform" action="form-to-email.php">

    <h1>June/July Crops: </h1>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check1">Pink Lady Apples  2kg <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check2">Seedless Imperials Mandarines 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check3">Bananas 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check4">Seedless Thompson Grapes 1kg <b>$6.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check5">Strawberries <b> TBA</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check6">Cherries <b> TBA</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check7">Pistachio Nuts 1/4kg <b>$5.00</b> </h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check8">Cashew Nuts 1/4kg <b>$5.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check9">Tassie Dutch Cream Potatoes 2kg <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check10">Avocado's 4 for <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check11">Tomatoes 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check12">Cold Pressed Honey 1 kg <b>$11.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check13">Cage Free Eggs 1 Doz <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check14">Soup Packs <br>(Carrot, parsley, onion, parsnip, celeriac) <b>$4.00</b></h2>
<input class="send" type="submit" value="Submit">
                    <input class="send" type="reset" value="Reset">
    </section>



    <section class="form">
    <h1>Order:</h1>
    <h2 style="margin-top:-15px;">To avoid delays pre-order here. Simply choose your crops, fill in your details below and click submit. </h2><br>

            Full Name:<br>
                <input class="name-email" type="text" name="name" placeholder="John Smith"><br>
            Business:<br>
                <input class="name-email" type="text" name="business" placeholder="Business Inc." ><br>
            Email:<br>
                <input class="name-email" type="text" name="email" placeholder="john.appleased@gmail.com"><br>
            Message<br>
                <input class="name-email" type="text" name="message" placeholder="Extra Comments" ><br>


        </form>

PHP:

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = '';//<== update the email address
$email_subject = "New Order submission";
$email_body = "You have received a new order from: $name.\n".
    "Email:$email_from: /n"
    "Business Name:  $business.\n".
    "Here is the message: $message\n".
    "PinkLadyApples2kg:$check1\n".
    "Mandarines1kg:$check2\n".
    "Bananas1kg:$check3\n".
    "Grapes1kg:$check4\n".
    "Strawberries:$check5\n".
    "Cherries:$check6\n".
    "PistachioNuts1.4kg:$check7\n".
    "CashewNuts1/4kg:$check8\n".
    "TassieDutchPotatoes2kg:$check9\n".
    "Avocados4:$check10\n".
    "Tomatoes1kg:$check11\n".
    "Honey1doz:$check12\n".
    "Eggs1doz:$check13\n".
    "Soup:$check14\n".

$to = "";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header( 'Location:/thank-you.html' );


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 
4

3 回答 3

1

研究这个关于解析错误的链接https://addons.mozilla.org/en-US/firefox/addon/web-developer/?src=search。这基本上是一个语法错误。在您忘记使用诸如 , ; 之类的文字的地方 ““ ' ETC。

于 2013-06-10T06:05:51.137 回答
0

我认为您的代码中有 header('Location: ....') 但在此之前有 echo 或者在<?phpor之前有一些东西<?。可以在输出某些内容之前发送标头。您可以通过删除回声或使用缓冲区来修复它。

<?php
ob_start();

<YOUR CODE>

ob_end_flush();

?>
于 2013-06-10T06:17:20.877 回答
0

该错误Headers already sent意味着您header()在 PHP 被强制发送 HTTP 标头后尝试使用;echo这通常是因为某些代码在您调用之前尝试了某些东西header()(标头必须在数据之前发送)。

  1. 检查预先运行的代码中的echo语句、对 的调用print()以及发送输出的任何内容。
  2. 检查代码文件的开头。它之前是否有空格或换行符<?php?这将强制 PHP 发送标头。
  3. 检查包含文件中的echoes、print()s 和 . 之前的空格<?php。还要检查包含文件的?>. (最后一个特别讨厌,因为它很容易被忽视。)

您可以通过在代码之前和之后调用来阻止对标头的调用echoprint()发送标头。这也将解决它们之间的任何问题。(警告:发送数据,所以以后不能调用。调用的最佳位置是在代码文件的末尾。)ob_start()ob_end_flush()?> ob_end_flush()header()ob_end_flush()

于 2013-06-10T06:29:40.910 回答