开发者

JSON message returned to HTML form

I have an HTML form that posts values to a web ser开发者_如何学编程vice that sends back a JSON status or error message. This form is embedded in Wordpress. How might I access the returned value and display an error message?


It would involve some Javascript, I'd highly recommend using jQuery with it's ajax function:

;(function($) {
  $(document).ready(function() {
    $('#form-id').bind('submit', function(e) {
      e.preventDefault();
      $.ajax({
        type: 'post',
        url: $('#form-id').attr('action'),
        dataType: 'json',
        success: function(jsonObject) {
          if (jsonObject.error != undefined) {
            alert(jsonObject.error.message);
          }
          else {
            alert('The submission was successful');
          }
        },
        error: function() {
          alert('A connection error occurred. Please try again');
        }
      });
    });
  });
})(jQuery);

That will make an HTTP post to the URL contained in the form's action attribute, and load the returned JSON string into a javascript object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜