我已经为我的单页投资组合的联系表单编写了一些 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>";
}
?>