我有一个PHP联系表单,目前会将您带到一个单独的页面。我希望“成功/错误消息”出现在jQuery对话框中,而不是将您重定向到另一个页面。或者,如果我可以将其定位到DIV,然后可以在提交时显示它,我只需要在iframe以外的同一页面上显示结果。
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "info@gmail.com";
$email_subject = "Message From Testing.com";
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.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // not 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 First Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do 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 = 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 .= "Telephone: ".clean_string($telephone)."\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 own success html here -->
<html>
<div id="dialog" title="Thank You">
<p>Thank you for contacting us. We will be in touch with you very soon.</p>
</div>
</html>
<?php
}
?>
这是我的HTML。
<form name="contactform" method="get" autocomplete="on">
<input type="text" class="contact-form- input" name="name" placeholder="Name">
<input type="text" class="contact-form-input" name="email" placeholder="Email">
<input type="text" class="contact-form-input" name="telephone" placeholder="Phone">
<textarea rows="5" cols="50" class="contact-form-textarea" name="message" placeholder="Message"></textarea>
<input type="button" class="form-submit-button" name="send" onclick="mail();" value="Send">
<input type="reset" class="form-submit-button" name="clear" value="Clear">
</form>
答案 0 :(得分:0)
提交表单后无法执行此操作,您必须在提交表单之前执行此操作。向表单添加一个onClick事件,指向具有AJAX请求的函数。然后在成功时显示弹出窗口。
<script type="text/javascript">
function mail() {
var email = jQuery("#email").val();
var name = jQuery("#name").val();
var telephone = jQuery("#telephone").val();
var message = jQuery("#message").val();
var postData = { email: email, name: name, telephone: telephone, message: message };
jQuery.post('mail.php',
postData,
function ( jsonResults ) {
jQuery("#dialog").show();
},
'text'
);
}
</script>
<form id="mail-form" method="get">
...
<input type="button" value="Get" onclick="mail();" />
</form>