PHP web表单的电子邮件


PHP web form to email

我已经在PHP中设置了一个电子邮件web表单。表单正常工作并发送电子邮件。但它没有重定向,而是出现了这个错误:error;您需要提交表单!警告:无法修改标头信息-标头已经在/home/con17120/public_html/form-to-email.php第70行发送(输出开始于/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;
  }
}
?> 

研究这个链接,了解解析错误https://addons.mozilla.org/en-US/firefox/addon/web-developer/?src=search。这基本上是一个语法错误。当你忘记使用字面意思的时候,比如;

我认为你有头('Location: ....')在你的代码,但你有回声之前,或者你有<?php<?之前的东西。可以在输出内容之前发送报头。您可以通过删除回显或使用缓冲区来修复它。

<?php
ob_start();
<YOUR CODE>
ob_end_flush();
?>

错误Headers already sent意味着你已经尝试使用header()后PHP已被迫发送HTTP头;这通常是因为一些代码在调用header()之前试图echo(头必须在数据之前发送)。

  1. 检查echo语句,print()调用,任何发送输出的代码。
  2. 检查代码文件的开始部分。在<?php之前有空格或换行符吗?这将强制PHP发送头文件。
  3. 检查包含的echo es, print() s和<?php之前的空间文件。还要检查所包含文件的?>后面是否有空格和换行符。(最后一个特别讨厌,因为它很容易被忽视。)

可以通过在代码前调用ob_start(),在代码后调用ob_end_flush()来防止对echoprint()的调用发送头消息。这也将修复它们之间任何?>的问题。(警告: ob_end_flush()正在发送数据,因此您之后无法调用header()。调用ob_end_flush()的最佳位置是在代码文件的末尾。