drkxgs9358 2018-03-29 02:01
浏览 50
已采纳

jQuery Post在Error上检索PHP echo

I am trying to create a popup modal that will display text echoed from a php submission script. So far it works for a success and prints the echoed content but I also want to catch echoed messages when an exception occurs in the php script.

popnotify is a seperate js function that takes text input and displays the modal with the text as a paragraph.

createuser.php

try:
    ......
    echo 'Successfully created a new user';
}
catch (PDOException $ex){
    echo "Something went wrong when creating a new user";
    http_response_code(403);
    die();
}

.js

jQuery("#load_form").on("click","#create_submit",function(event){
event.preventDefault();
var to_submit = $("#s_form").serialize();
var formLink = $("#s_form").attr('action');
jQuery('#load_form').find('input, select, button').prop('disabled', true);

$.post(formLink,to_submit)
.done(function(data){
    popnotify_success(data);
})
.fail(function(data){ 
    popnotify_fail(data);
})
.always(function(){
    jQuery('#load_form').find('input, select, button').prop('disabled', false);  
});  });
  • 写回答

1条回答 默认 最新

  • dsgw8802 2018-03-29 03:06
    关注

    How about using a flag to determine if there were errors

    Your PHP, createuser.php

    try {
        // Some more code here that tries to create the user
        $response = [
            'error' => 0,
            'message' => 'Successfully created a new user',
        ];
        echo json_encode($response);
    }
    catch (PDOException $ex){
        $response = [
            'error' => 1,
            'message' => 'Something went wrong when creating a new user',
        ];
        echo json_encode($response);
        // HTTP Code 200 OK, there were not HTTP errors or server (500) errors
        // http_response_code(403);
        die();
    }
    

    Your JavaScript

    jQuery("#load_form").on("click","#create_submit",function(event){
      event.preventDefault();
      var to_submit = $("#s_form").serialize();
      var formLink = $("#s_form").attr('action');
      jQuery('#load_form').find('input, select, button').prop('disabled', true);
    
      $.post(formLink,to_submit)
      .done(function(data){
        // Parse the result into an object
        var result = JSON.parse(data);
    
        // Get the error flag from the object
        if (result.error == 0) {
    
          // There was no error
          popnotify_success(result.message);
    
        } else {
    
          // There was an error
          popnotify_fail(result.message);
    
        }
      })
      .fail(function(data){ 
        // Handle http errors here
      })
      .always(function(){
        jQuery('#load_form').find('input, select, button').prop('disabled', false);  
      });  
    });
    

    You could also use a generic popnotify() function, instead of two, if all the function does is create a notification. Any other code dependent on error/success could go on the handler function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效