Javascript HTML表单可以';不要发送到PHP文件
我在我的网站上有一个联系表,我想发送到一封电子邮件,我把它设置在“onsubmit”的位置,它将发送到一个js函数,并检查是否所有输入都已填写。然后,转到我的php文件。然而,在我点击提交后,它说,“这个页面不工作”。我的JS函数工作得很好,只是我的php没有 contact.htmlJavascript HTML表单可以';不要发送到PHP文件,javascript,php,html,Javascript,Php,Html,我在我的网站上有一个联系表,我想发送到一封电子邮件,我把它设置在“onsubmit”的位置,它将发送到一个js函数,并检查是否所有输入都已填写。然后,转到我的php文件。然而,在我点击提交后,它说,“这个页面不工作”。我的JS函数工作得很好,只是我的php没有 contact.html <p id="required">Required <span style="color: red;">*</span></p> <script sr
<p id="required">Required <span style="color: red;">*</span></p>
<script src="js/formvalidate.js"></script>
<form class="contact-form" name="form" action="php/contact.php" onsubmit="return formValidate()" method="post">
<label for="first_name" id="label_first" class="contact-label">First Name<span class="asterisk" id="required_fname"></span></label><br>
<input type="text" id="first_name" name="first_name" class="contact-input" placeholder="First Name"><br>
<label for="last_name" id="label_last" class="contact-label">Last Name<span class="asterisk" id="required_lname"></span></label><br>
<input type="text" id="last_name" name="last_name" class="contact-input" placeholder="Last Name"><br>
<label for="email" id="label_email" class="contact-label">Email<span class="asterisk" id="required_email"></span></label><br>
<input type="email" id="email" name="email" class="contact-input" placeholder="Email"><br>
<label for="subject" id="label_subject" class="contact-label">Subject<span class="asterisk" id="required_subject"></span></label><br>
<input type="text" id="subject" name="subject" class="contact-input" placeholder="Subject"><br>
<label for="message" id="label_message" class="contact-label">First Name<span class="asterisk" id="required_message"></span></label><br>
<textarea id="message" name="message" class="contact-message" placeholder="Type here..."></textarea><br>
<button type="submit" id="submit" name="submit" class="submit">Submit</button>
</form>
contact.php
<?php
if (isset($_POST['submit'])) {
$fname = $_POST['first_name'];
$lname = $_POST['last_name']
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "database@terrytowell.com";
$headers = "From: ".$mailFrom;
$name = $fname . " " . $lname;
$txt = "You have received an Email from " . $name . "\n\n" .$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: ../contact.html?mailsend");
}
?>
这行代码可以帮助您: 首先在您的函数旁边插入此函数
function send_mail($to, $from, $title, $message) {
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $title, $message, $headers);
}
然后在变量后面调用它
send_mail($_POST['receiver-email'], $_POST['title(maybe website email)'], "title", "here some other post data from your choice"));
如果验证函数成功,您需要返回一个值“My JS function works Perfect”。不,您的函数永远不会返回true。
此页面不工作
是一个常见的PHP错误。您需要查看服务器错误日志,找出它抛出错误的原因。我猜是$lname=$\u POST['last\u name']
结尾缺少的分号可能只是一句警告的重复。处理POST请求和发送电子邮件的PHP代码在检查提供的数据方面几乎没有什么作用——客户端验证代码很容易被否定,元素上的属性被修改,然后PHP就会失败。
send_mail($_POST['receiver-email'], $_POST['title(maybe website email)'], "title", "here some other post data from your choice"));