联系表格,发送电子邮件只是下载我的PHP文件



我遇到了一个奇怪的错误,我无法用PHP文件来解决,我从未接触过PHP,我正试图用它从网站上向我发送姓名、电子邮件地址和消息,但出于某种原因,我写的代码只是下载PHP文件,其他人能看到我可能在哪里出错吗:

HTML

<div class="modal-body">
<div class="container"> <!-- TODO add in contact form -->
<form action="form-to-action.php">

<label for="fname">Name</label>
<input type="text" id="Name" name="Name" placeholder="Name">

<label for="lname">Email Address</label>
<input type="text" id="Email" name="Email" placeholder="Email Address">
<br>

<label for="subject">How can we Help?</label>
<textarea id="Message" name="Message" placeholder="Write something.." style="height:200px"></textarea>

<input type="submit" value="Send"id="contactBtnModal"style="text-decoration: none;" >

</form>
</div>
</div>

PHP:

<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "testingemail@gmail.com";
$email_subject = "Testing";
function problem($error)
{
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br><br>";
echo $error . "<br><br>";
echo "Please go back and fix these errors.<br><br>";
die();
}
// validation expected data exists
if (
!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // required
$message = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email address you entered does not appear to be valid.<br>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br>';
}
if (strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($error_message);
}
$email_message = "Form details below.nn";
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "n";
$email_message .= "Email: " . clean_string($email) . "n";
$email_message .= "Message: " . clean_string($message) . "n";
// create email headers
$headers = 'From: ' . $email . "rn" .
'Reply-To: ' . $email . "rn" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

您的计算机中是否安装了PHP服务器?

如果没有,请下载WAMP并重试,您需要通过http://localhost,而不是通过file://path_to_document在您的web导航器中。

或者在web服务器中运行脚本。

最新更新