提交后,php表单不会发送或转到标题位置

问题描述:

I have a simple php form for contact purposes. However, it won't send the email or go to the correct page after submit. It redirects to the mail.php instead.

My contact form named contact.php is as follows:

 <form id="contact-form" action="mail.php" method="POST">
    <fieldset>

        <label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
        <label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
        <label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
        <div class="wrapper">
            <div class="text-form">Your Message:</div>
            <textarea name="message" rows="6" cols="25"></textarea><br />
            <div class="clear">
        </div>
        <div class="buttons">
        <a class="button" href="#"><input class="button" type="submit" value="Send"></a>
        <a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
        </div>

    </fieldset>
</form>

And the php code named mail.php is as follows:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    &contact = $_POST['contact'];
    $formcontent="From: $name 
 Message: $message";
    $recipient = "info@whatever.co.za";
    $subject = "Contact form message";
    $mailheader = "From: $email 
";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location:contact.php");
    ?>

What am i doing wrong. It just wont send the message to email or redirect to the correct page??

我有一个简单的php表单用于联系目的。 但是,它不会在提交后发送电子邮件或转到正确的页面。 它改为重定向到mail.php。 p>

我的联系表格名为contact.php如下: p>

 &lt; form id =  “contact-form”action =“mail.php”method =“POST”&gt; 
&lt; fieldset&gt; 
 
&lt; label&gt;&lt; span class =“text-form”&gt;您的姓名:&lt;  /跨度&GT;  &lt; input type =“text”name =“name”&gt;&lt; / label&gt; 
&lt; label&gt;&lt; span class =“text-form”&gt;您的电子邮件:&lt; / span&gt;&lt;输入类型 =“text”name =“email”&gt;&lt; / label&gt; 
&lt; label&gt;&lt; span class =“text-form”&gt;您的联系人号码:&lt; / span&gt;&lt; input type =“text  “name =”contact“&gt;&lt; / label&gt; 
&lt; div class =”wrapper“&gt; 
&lt; div class =”text-form“&gt;您的留言:&lt; / div&gt; 
&lt;  ; textarea name =“message”rows =“6”cols =“25”&gt;&lt; / textarea&gt;&lt; br /&gt; 
&lt; div class =“clear”&gt; 
&lt; / div&gt; \  n&lt; div class =“buttons”&gt; 
&lt; a class =“button”href =“#”&gt;&lt; input class =“button”type =“submit”value =“Send”&gt;&lt;  / a&gt; 
&lt; a class =“button”href =“#”&gt;&lt; input class =“button”type =“reset”value =“Clear”&gt;&lt; / a&gt; 
&lt; /  div&gt; 
 
&lt; / fieldset&gt; 
&lt; / form&gt; 
  code>  pre> 
 
 

名为mail.php的php代码如下: p>

 &lt;?php 
 $ name  = $ _POST ['name']; 
 $ email = $ _POST ['email']; 
 $ message = $ _POST ['message']; 
&amp; contact = $ _POST ['contact']; \  n $ formcontent =“From:$ name 
 Message:$ message”; 
 $ recipient =“info@whatever.co.za”; 
 $ subject =“联系表单消息”; 
 $ mailheader =“From  :$ email 
 
“; 
邮件($ recipient,$ subject,$ formcontent,$ mailheader)或死(”错误!“); 
标题(”位置:contact.php“); 
?  &gt; 
  code>  pre> 
 
 

我做错了什么。 它只是不会将消息发送到电子邮件或重定向到正确的页面? p> div>

Your syntax is ever so slightly wrong. &contact on line 5 should be $contact. I assume you're on a production server, so error reporting would be disabled and you wouldn't get any warnings.

Please check the contact.php file whether some redirection is happening from contact.php to mail.php

I think there may be an issue sending mail from your server. The mail.php file is probably tripping up on the mail function and not doing the redirect.

Create a new file with just the php mail function in there (with your email address and a test message) and browse it from your web browser, does it send?

Try using @ before mail function

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name 
 Message: $message";
$recipient = "info@whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email 
";
@mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>

this will let you pass even if mail function produce any issue. Also please check if there are any other header are not sent using http://php.net/manual/en/function.headers-sent.php function.

I would strongly suggest to use PHPMailer or some other class to send any kind of email.

Try to use ob_start() at the start of script, and exit(); after header("Location: contact.php");

smth like this ...

<?php
    ob_start();
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    &contact = $_POST['contact'];
    $formcontent="From: $name 
 Message: $message";
    $recipient = "info@whatever.co.za";
    $subject = "Contact form message";
    $mailheader = "From: $email 
";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: contact.php");
    exit();
?>

Change the mail.php to the following:

<?php
mail("info@whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
header("Location: contact.php");
?>