我有一个简单的 html 表单,其中包含一个提交给 download.php 的字段,该表单验证表单并最终向我发送一封包含用户输入的电子邮件。到目前为止,一切都很好 - 但我希望错误消息和感谢消息显示在原始 html 表单中,而不是在新页面上(就像现在发生的那样)。
你能告诉我如何在单击提交按钮后保持 myform.html 可见,并在“通知”div 中加载错误和感谢消息吗?
顺便说一句,以防万一,myform.html 使用 jQuery .load() 在 index.html 上动态显示。
谢谢你的帮助!
我的表格.html
<html>
<head>...</head>
<body>
<div id="notification"></div> <!-- want to load errors and thank you, here -->
<form id="downloadForm" method="post" action="_php/download.php">
<label for="biz_email">Business Email:</label><input type="text" name="biz_email">
<div id="submit" class="submit_button"></div>
<!-- FYI submit function called in separate jQuery file -->
</form>
</body>
</html>
下载.php
<?php
if(isset($_POST['biz_email'])) {
$email_to = "foo@bar.com"; //my email
$email_subject = "form submission";
$email_from ="form test";
$biz_email = $_POST['biz_email'];
$error_message = "";
// validation
...
//error code
function died($error) {
echo "There are errors in your submission:<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
//email message (sent back to me)
$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 .= "Email: ".clean_string($biz_email)."\n";
// create email headers
$headers = 'From: '.$biz_email."\r\n".
'Reply-To: '.$biz_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- thank you message -->
Success!
<?php
}
?>