<?php
session_start();
if($_POST["captcha"]==$_SESSION["captcha_code"]){
$email_to = "123@gmail.com";
$email_subject = "Mail from contact form";
$name = $_POST['name']; // required
$topic = $_POST['topic']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$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 From : ".clean_string($email_from)."\n";
$email_message .= "Contact no: ".clean_string($telephone)."\n";
$email_message .= "Regarding: ".clean_string($topic)."\n";
$email_message .= "message: ".clean_string($message)."\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);
echo "<p class='success'>Your message has been sent. Thank you!</p>";
} else {
print "<p class='Error'>Enter Correct Captcha Code.</p>";
}
?>
在检查了capthca之后,这是从我的php文件返回的。
这是我在另一个文件中的脚本。经过php中的capthca验证后,它将返回html数据。成功验证后,我需要重置表单。
function sendContact() {
var valid;
valid = validateContact();
if(valid) {
jQuery.ajax({
url: "contact_mail.php",
data:'name='+$("#name").val()+'&email='+$("#email").val()+'&phone='+$("#phone").val()+'&topic='+$("#topic").val()+'&message='+$("#message").val()+'&captcha='+$("#captcha").val(),
type: "POST",
success:function(data){
$("#mail-status").html(data);
},
error:function (){}
});
}
}
我尝试过
success:function(data){
$("#mail-status").html(data);
$("#name").val('');
$("#email").val('');
$("#phone").val('');
$("#topic").val('');
$("#message").val('');
$("#captcha").val('');
$("#captcha_code").attr('src','captcha_code.php');
}
},
error:function (){}
});
但是即使capthca错误,它也会重置。表示任何来自php的回复都将被重置。任何解决方案
答案 0 :(得分:1)
尝试此代码-
<?php
session_start();
if($_POST["captcha"] == $_SESSION["captcha_code"]) {
$email_to = "123@gmail.com";
$email_subject = "Mail from contact form";
$name = $_POST['name']; // required
$topic = $_POST['topic']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$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 From : ".clean_string($email_from)."\n";
$email_message .= "Contact no: ".clean_string($telephone)."\n";
$email_message .= "Regarding: ".clean_string($topic)."\n";
$email_message .= "message: ".clean_string($message)."\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);
echo json_encode("success" => "Your message has been sent. Thank you!");
} else {
echo json_encode("error" => "Enter Correct Captcha Code");
}
?>
并成功执行ajax(使用dataType json)-
dataType: 'json',
success:function(data){
if(data.success){
$("#form-id").trigger("reset");
}elseif(data.error){
alert(data.error);
}
error:function (){}
});
答案 1 :(得分:0)
尝试此代码:
$("#form-id")[0].reset();
答案 2 :(得分:0)
尝试一下:
function sendContact() {
var valid;
valid = validateContact();
if(valid) {
jQuery.ajax({
url: "contact_mail.php",
data:$("#form_id").serialize(),
type: "POST",
success:function(data){
$('#form_id').trigger("reset");
$("#mail-status").html(data);
},
error:function (){}
});
}
}