php - jQuery .post how to add Failure -


i have below script , works want add alert when went wrong. part of drupal module.

my short php:

ajax_function() {     //below failure part. want display message     drupal_json(array('status' => 'failure', 'message' => 'you cannot submit this.')); } 

the js:

button.click(function(e) {     e.preventdefault();     var path = 'to/ajax/function';     $.post(path, function(data) {         //submits on success         $('#webform').submit();     }); }); 

you can use: .done succes, .fail error , .always finished

var yourpost = $.post(path, function(data) {     //submits on success     $('#webform').submit(); })  .done(function() {     alert( "second success" ); }) .fail(function() {     alert( "error" ); }) .always(function() {     alert( "finished" ); }); 

please see: http://api.jquery.com/jquery.post/

update

you can value of array. this.

button.click(function(e) {     e.preventdefault();     var path = 'to/ajax/function';     $.post(path, function(data) {         //submits on success         if(data.status == "failure")             //your can "data.message" contain message         else             $('#webform').submit();     }); }); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -