javascript - How to extract Ajax returned info in this particular code? -
i have 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 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 form message $(form).replacewith($(message)); }, error: function() { var error = $('<div class="alert alert-error">error </div>'); $(form).replacewith(error); } }); } }); }); };
supported messagetypes are: 'success' , 'error' css load style
and code in server:
$d= array( 'status' => 'success', 'm.status'=> 'success', 'message'=>'success' ); echo json_encode($d); ?>
but returns no success message. how should form array in server side?
i know enters success section of code don't know how write variables in server side.
the javascript code should not changed, have modify server php. how can this?
also have few questions:
should variable name in server php same variable name inside function? in example when echo $d should
success: function(d) {
or can use other names well? related?when variables 'k' , 'm' in
function(k, m){
coming from? can explain function does?
start longest mile
(dunno if called that, stuck me) witch console.log('something');
should see , gradually moving backwards on execution until see something, nice easy way isolate problem.
also if using google chrome
dev tools has nice tab network, can see ajax request , returned. maybe returned error 500?
this way can diagnose ajax call.
as questions:
no, jquery parameter name variable results. can name anything, use
resp
personally.the parameters in each refer "key" , "value" (in case using m, above). read here more info on that.
Comments
Post a Comment