0

我已经为我的单页投资组合的联系表单编写了一些 PHP 代码,当它提交时它只是打开一个空白页面,并且电子邮件没有发送并且文本没有回显。我对 PHP 不是很有信心,因为它对我来说相当新。如果有人可以提供帮助,那就太好了?

html -

<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
        <li>
            <label for="name">Name</label>
            <input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
        </li>
        <li>
            <label for="email">Email</label>
            <input id="email" name="email" type="email" placeholder="example@domain.com" required>
        </li>
        <li>
            <label for="phone">Phone</label>
            <input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
        </li>
        <li>
            <label for="subject">Subject</label>
            <input id="subject" name="subject" type="text" placeholder="What the message is about" required>
        </li>
        <li>
        <label for="message">Message</label>
        <textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
        </textarea>
        </li>
          <li>
        <label for="human">What is 2 + 2 ?</label>
        <input id="human" name="human" type="number" placeholder="Please insert answer" required>
        </li>
    </ol>
</fieldset>
<fieldset>
    <input class="button" id="submit" type="submit" value="Send it!">
</fieldset>
</form>

php -

<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: $email'; 
$to = 'ben_humphries@hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$human = $POST['human'];    

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { //mail sends it to the SMTP server side which sends the email
    echo "<p>Your message has been sent!</p>";
} else { 
    echo "<p>Something went wrong, go back and try again!</p>"; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>
4

1 回答 1

0

您的表单和处理程序有一些问题。

在您的 HTML 表单中,您没有输入,human所以我补充说,加上您的submit按钮没有命名,因此仅此一项是行不通的。

<input class="button" id="submit" name="submit" type="submit" value="Send it!">

在您的 PHP 处理程序中,您(mail headers)的格式不正确,因此最终出现在我的垃圾邮件框中,所以我也添加了它。

$from = $_POST['email'];

以及标题加上我将您的最后一个条件固定为:

if (!isset($_POST['submit']) && ($_POST['human']) != '4')

HTML 表单

<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
    <li>
        <label for="name">Name</label>
        <input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
    </li>
    <li>
        <label for="email">Email</label>
        <input id="email" name="email" type="email" placeholder="example@domain.com" required>
    </li>
    <li>
        <label for="phone">Phone</label>
        <input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
    </li>
    <li>
        <label for="subject">Subject</label>
        <input id="subject" name="subject" type="text" placeholder="What the message is about" required>
    </li>


      <li>
    <label for="human">What is 2 + 2 ?</label>
    <input id="human" name="human" type="number" placeholder="Please insert answer" required>
    </li>

    <li>
    <label for="message">Message</label>
    <textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
    </textarea>
    </li>
    </ol>
</fieldset>
<fieldset>
    <input class="button" id="submit" name="submit" type="submit" value="Send it!">
</fieldset>
</form>

PHP 处理程序,经过测试并为您工作。

<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = $_POST['email'];
$to = 'ben_humphries@hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

if(isset($_POST['submit']) && ($_POST['human']) == '4') {               
mail ($to, $subject, $body, $headers);  //mail sends it to the SMTP server side which sends the email
    echo "<p>Your message has been sent!</p>";
} 

else { 
    echo "<p>Something went wrong, go back and try again!</p>"; 
} 
if (!isset($_POST['submit']) && ($_POST['human']) != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>
于 2013-11-12T20:35:41.467 回答