Reputation: 4818
I have following js code, which is validating a form calling a php script at backend:
$(function() {
// Setup form validation on the #register-form element
$("#register_form").validate({
// Specify the validation rules
rules: {
register_username: "required",
register_password: "required",
register_email: {
required: true,
register_email: true
},
register_confirm_password: {
required: true,
minlength: 5
},
},
// Specify the validation error messages
messages: {
register_username: "Please enter your username",
register_password: "Please enter your password",
register_confirm_password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
register_email: "Please enter a valid email address",
},
submitHandler(function(form) {
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: $(this).serialize(),
dataType : 'json',
success(function(data) {
if (data){
alert("success");
// $(this)[0].reset();
}
})
});
return false;
});
});
});
At the backend I have following code:
var_dump($_POST);
The issue is in browser, I am successfully able to dump php data, but unable to call this function message in js:
submitHandler(function(form) {
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: $(this).serialize(),
dataType : 'json',
success(function(data) {
if (data) {
alert("success");
// $(this)[0].reset();
}
})
});
return false;
});
Please help me to solve the above code.
Upvotes: 0
Views: 47
Reputation: 19212
The problem is that var_dump($_POST);
wont output valid JSON. Change it to this:
echo json_encode($_POST);
Upvotes: 2