为什么我的PHP/HTML表单不向我发送数据
在多年的不活动之后,我正在尝试PHP,我以为我已经有了它,但看起来我失去了联系。有人知道为什么我没有收到任何发送到我电子邮件地址的数据吗 HTML代码:为什么我的PHP/HTML表单不向我发送数据,php,html,Php,Html,在多年的不活动之后,我正在尝试PHP,我以为我已经有了它,但看起来我失去了联系。有人知道为什么我没有收到任何发送到我电子邮件地址的数据吗 HTML代码: <form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate"> <div class="col-md-6"> <
<form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate">
<div class="col-md-6">
<input id="name" type="text" placeholder="Name" name="name">
<input id="email" type="text" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<input id="phone" type="text" placeholder="Phone" name="phone">
<input id="subject" type="text" placeholder="Subject" name="subject">
</div>
<div class="col-md-6">
<button type="submit" value="submit" class="thm-btn"><a href="https://rootlayertechnologies.com.au/callbackrequest.php">submit now</a></button>
</div>
<div class="col-md-12">
<div id="success"></div>
</div>
</form>
PHP代码:
<?php
if (isset($_POST['email'])) {
$email_to = "xxxxx@xxxx.com";
$email_subject = "Call Back Request Form - Home Page | rootlayertechnologies.com.au";
function died($error) {
// your error code can go here
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 and submit the form again.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['subject']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$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($subject) < 2) {
$error_message .= 'The Subject you entered does not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = [
"content-type",
"bcc:",
"to:",
"cc:",
"href"
];
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Subject: " . clean_string($subject) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n"
. 'Reply-To: ' . $email_from . "\r\n"
. 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<p>Thanks for contacting us. We have received your request for a call back and a friendly member of our Product Solutions team will be in touch with you soon.</p>
<?php}?>
谢谢联系我们。我们已收到您的回电请求,我们产品解决方案团队的一位友好成员将很快与您联系
我将首先移除
按钮内的
标记。我认为通过点击链接,你可能只是打开页面而不是提交表单
然后,在PHP代码中,我将确认$email\u to
设置正确。(这似乎很明显,但始终值得确认。)
我无法测试代码,但我没有发现任何错误本身。(有些方面可以改进,例如缺少filter\u var
或者html\u escape
,addslashes
,或者strip\u标签
)
这使我认为问题可能出在服务器级别。因此,这里有一些事情需要考虑:
必须是与站点来自同一域的地址(例如。info@rootlayertechnologies.com.au). 不过,请保持原样回复:From:
- 确保SPF记录设置正确。您可能还想深入研究DKIM和DMARC
- 一些托管公司禁用PHP
。您可能需要实现SMTPmail
@mail
-您正在阻止发送电子邮件时出现的错误消息。如果有问题,你怎么知道?不要在PHP中使用@
。您的PHP不会向浏览器输出任何内容。您应该进行测试,以查看是否输入了if语句,以及传递给mail
的变量的值最终是什么。请遵循中的建议。尤其是标题为“极小”的部分。通过调试找出问题所在。不要只是看着整个剧本,然后用一句“它不起作用!”把你的手举到空中。这似乎就是问题所在。非常感谢。现在我只有一个错误,提交的表单没有到达指定的收件箱。有什么想法吗?谢谢。至少我们正在取得进展。我现在将深入研究PHP代码并更新我的答案。