php - 联系表格,发送电子邮件只是下载我的 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.\n\n";
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 . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'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 访问该页面,而不是通过 web 导航器中的 file://path_to_document 访问该页面。
或者在 Web 服务器中运行脚本。
推荐阅读
- algorithm - 证明 n=o(2^{f(n)})?
- vb.net - 如何删除列表框数据源中的重复值?
- google-apps-script - 不隐藏基于特定单元格的所有空行
- python - 如何在 Microsoft Azure Functions 中重新启动 Python 运行时
- c++ - 如何在不浪费电流的情况下使用下拉电阻让 SPI SCK 线空闲到低电平?
- reactjs - React Store 不使用 React.useReducer 更新上下文
- azure - 在 Azure 管道的 NuGet 包操作中使用自定义环境变量
- javascript - 循环和使用document.write时如何在点击时发送唯一ID
- ruby-on-rails - react-rails heroku 图像/视频未显示
- java - 使用 AssertJ 比较 Java 中的 2 个 csv