<?php
session_start();
if($_POST["captcha"]==$_SESSION["captcha_code"]){
$email_to = "[email protected]";
$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.nn";
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."rn".
'Reply-To: '.$email_from."rn" .
'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>";
}
?>
This is returned from my php file after the capthca is checked.
This is my script in another file. After the capthca validation in php it returns the html data. I need to reset the form after successful validation.
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 (){}
});
}
}
I tried this
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 (){}
});
But it resets even when the capthca is wrong. Means any reply from php the form get resets. Any solutions
3
Answers
try this code-
and in ajax success (use dataType json)-
Try this code :
Try This: