How to extract Ajax retured info in this particular code?
I have this code in client side:
this.formValidation = function() {
// Parse forms
$('.submit.btn').on('click', function(){
$(this).closest('form').submit();
});
$.each($('form.validate'), function(){
$(this).validate({
submitHandler: function(form) {
var data = $(form).serializeArray();
var action = $(form).attr('action');
$.ajax({
method: 'post',
dataType: 'json',
url: action,
data: data,
success: function(d) {
// Prepare the message
var message = '';
$.each(d, function(k, m){
var messageType = 'boolean' ===
$.type(m.status) ?
(m.status?'success':'error') : m.status;
message += '<div class="alert
alert-'+messageType+'">'+m.message+'</div>';
});
// Replace the form with the message
$(form).replaceWith($(message));
},
error: function() {
var error = $('<div class="alert
alert-error">error </div>');
$(form).replaceWith(error);
}
});
}
});
});
};
supported messagetypes are: 'success' and 'error' for the css to load the
style
and this code in server:
$d= array(
'status' => 'success',
'm.status'=> 'success',
'message'=>'success'
);
echo json_encode($d);
?>
But it returns no success message. How should I form the array in server
side?
I know it enters the success section of the code but I don't know how to
write the variables in the server side.
The JavaScript code should not be changed, I just have to modify the
server php. How can I do this?
Also I have a few questions:
Should the variable name in server php be the same as the variable name
inside the function? in this example when I echo $d should it be like
success: function(d) { or I can use other names as well? Are they even
related?
When are the variables 'k' and 'm' in function(k, m){ are coming from? can
you explain what the function does?
No comments:
Post a Comment