I am using the validate plugin for a form. I just want a contactform with no refresh. I guess it is possible.
This is my Javascript:
$().ready(function() {
// validate the comment form when it is submitted.
$("#actualForm").validate({
focusInvalid: false,
onkeyup: true,
rules: {
// simple rule, converted to {required:true}
name: "required",
comment: "required",
email: {
required: true,
email: true
},
},
submitHandler: function(form) {
var name = $('input[name="name"]').val();
var email = $('input[name="email"]').val();
var comment = $('input[name="comment"]').val();
var params = {
"name": name,
"email": email,
"comment": comment,
};
console.log(data);debugger;
$.ajax({
url: 'email.php',
data: params,
type: 'POST',
dataType: json,
success: function (data) {
if (data.status) {
//Give success log to user
} else {
//Do something
}
}
});
}
And this is my PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$to = 'myemail';
$from = $email;
$subject = 'subject';
$body = "Hello Admin<br><br>
Name: <strong>$name</strong><br>
Email: $email<br>
Bericht: $comment<br>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: $from";
$ok = mail($to, $subject, $body, $headers);
if($ok)
echo '1';
else
echo '0';
?>
I also have an invalidHandler, but that thing is working so i guess it's useless to put it here.
But I am stuck for hours now and I hope someone could help me with this problem.
Your code have a little mistake inside the ajax function
replace:
dataType: json,
by:
dataType: 'json',
Without '', json is considered as a variable name, and in your case you want to pass 'json' as a value.
I have a tendency to use var_dump to debug.
What's var_dump($_POST); giving you?
Do you get a 500 network response in your browser?
Type console.log(data); in your success function too. That will give you a hint on how to solve the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.