【发布时间】:2022-01-01 05:15:56
【问题描述】:
我正在尝试使用 PHP 脚本创建一个简单的联系表单来发送电子邮件。但是,与 index.html 位于同一文件夹中的以下脚本不起作用。它引导我进入“此页面无法正常工作”页面。在这个 URL 上:ipaddress/contact-form.php。可能是我放错了文件,但似乎没有掌握必须做什么。
HTML
<form method="POST" action="contact-form.php" class="contact-form">
<label for="name" class="form-label">Volledige naam*</label>
<input
type="text"
name="name"
id="Name"
placeholder="Your full name"
required
/>
<label for="email" class="form-label">Email*</label>
<input
type="email"
name="email"
id="Email"
placeholder="voorbeeld@hotmail.com"
required
/>
<label for="phone" class="form-label">Telefoonnummer</label>
<input
type="tel"
name="phone"
id="Phone"
placeholder="06-12345667"
/>
<label for="message" class="form-label">Bericht*</label>
<textarea
type="text"
name="message"
id="Message"
placeholder="Uw bericht.."
required
></textarea>
<small class="required">* verplicht</small>
<button type="submit" class="submit">Verstuur</button>
</form>
<?php
if (isset($_POST['Email'])) {
$email_to = "xxxxxxxxx@gmail.com";
$email_subject = "Een nieuw contactbericht";
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();
}
if (
!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Phone']) ||
!isset($_POST['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$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 .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Bedankt voor uw bericht! We zullen zo spoedig mogelijk contact met u opnemen.
<?php
}
?>
有什么想法吗? :)
【问题讨论】:
-
“此页面无法正常工作” ...消息中是否有附带的 HTTP 状态代码?例如404、500 之类的?
-
不要把
@放在函数名前面,例如在您的@mail...中。这只是抑制了实际上可能对诊断问题有用的错误。 -
什么确切不起作用?您尝试过什么来解决问题?您的消息是否正确构建?呼叫
mail工作正常吗?是否有任何关于此的信息写入您的服务器的错误日志,或者可能写入服务器的邮件日志? -
当我点击提交按钮时,它会直接将我带到一个错误页面。无法回答您的具体问题,因为这个 PHP 和表单对我来说是新的。浏览器中的控制台显示有关以下内容的错误;服务器以 405 状态响应。
-
Small Point 虽然在 IF 中创建函数是合法的,但它并不是一个好的做法。原因,如果您不在 IF 中,则不会创建该函数,您可能不会发现该函数依赖于 IF。还有龙