doulu8537
2014-08-06 13:42
浏览 52

如何获得从php到jquery ajax的响应

Hi I am trying to echo out certain messages from the php code back to my ajax. But normally I would only have one echo message but this case I have 2. But I have no idea on how to assign each echo to one one .html()

$("#finish").submit(function(){
    $.ajax({
        type:"GET",
        url:"checkFinish.php",
        data: $("#finishProj").serialize(),
        success: function(data){
            $("#add_sucess").html();
            $("#add_err").html();
        }
    }
});


if(!empty($mile1) && $mile1Pay == 'unPaid'){
        $error = 'Payment Not Completed';
        echo $error;
}
if(!empty($mile2) && $mile2Pay == 'unPaid'){
        $error = 'Payment Not Completed';
        echo $error;
}
if(!empty($mile3) && $mile3Pay == 'unPaid'){
        $error = 'Payment Not Completed';
        echo $error;
}

if(empty($error)){
  $success = "Success";
 echo $success;
}

I would like my echo $error to go inside the $("#add_err").html(); and echo $success to be in the $("#add_sucess").html(); How do I specify it? Cause normally if I only have one thing to echo out I would just $("#add_sucess").html(data);

图片转代码服务由CSDN问答提供 功能建议

您好我正在尝试将某些消息从php代码回显给我的ajax。 但通常我只会有一条回音信息,但这种情况我有2.但我不知道如何将每个回声分配给一个 .html() < pre> $(“#finish”)。submit(function(){ $ .ajax({ type:“GET”, url:“checkFinish.php”, data:$( “#finishProj”)。serialize(), success:function(data){ $(“#add_sucess”)。html(); $(“#add_err”)。html(); } } }); if(!empty($ mile1)&amp;&amp; $ mile1Pay =='unPaid'){ $ error ='Payment not Completed'; echo $ error ; } if(!空($ mile2)&amp;&amp; $ mile2Pay =='unPaid'){ $ error ='付款未完成'; echo $ error; } if(! 空($ mile3)&amp;&amp; $ mile3Pay =='unPaid'){ $ error ='付款未完成'; echo $ error; } if(空($ error)){ $ success =“成功”; echo $ success; }

我希望我的 echo $ error 能够 进入 $(“#add_err”)。html(); echo $ success 位于 $(“#add_sucess”)。html(); 如何指定? 通常如果我只有一件事要回应我只会 $(“#add_sucess”)。html(data);

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duandaodao6951 2014-08-06 13:51
    已采纳

    I would return a JSON object back to my ajax. This way I can divide my messages up better.

    JavaScript

    $("#finish").submit(function(){
        $.ajax({
            type:"GET",
            url:"checkFinish.php",
            dataType: "JSON",//ajax now expects an JSON object to be returned
            data: $("#finishProj").serialize(),
            success: function(data){  
                //now that data is a JSON object, you can call the properties via data.prop              
                $("#add_sucess").html(data.success);
                $("#add_err").html(data.error);
            }
        }
    });
    

    PHP

    if(!empty($mile1) && $mile1Pay == 'unPaid'){
            $error = 'Payment Not Completed';
    }
    if(!empty($mile2) && $mile2Pay == 'unPaid'){
            $error = 'Payment Not Completed';
    
    }
    if(!empty($mile3) && $mile3Pay == 'unPaid'){
            $error = 'Payment Not Completed';           
    }    
    if(empty($error)){
      $success = "Success";   
    }
    
    echo json_encode(array("error" => $error, "success" => $success));//json_encode an associative array and echo it back to request
    
    exit();
    

    Just make sure you have $success and $error defined before, otherwise you'll probably get an error.

    打赏 评论
  • dpv21589 2014-08-06 13:48

    Pass the flag of success : 1 for success and error: 0 for error from server side. And at ajax success you can identify the response by checking data.res is 1 or 0. For example :

    On server :

    if($id > 0 )  // for success
    {
      // do other stuff
      $data['res'] = 1 ; 
    }
    else// for error
    {
      // do other stuff
      $data['res'] = 0 ; 
    }
    echo $json_encode($data);
    

    On Client side :

    success: function(data){
            if(data.res==1)
             {
               $("#add_sucess").html();// add success message
             }
           else
           {
                 $("#add_err").html();// add error message
           }
    
        }
    

    Note : - Don't forget to use dataType: "json", in your Ajax call.

    Update :- If you are setting the string in success than set the success message or error on error message. so you check with EMPTY check on client side like :

    if(data.success_msg != "")
    {
    $("#add_sucess").html(data.success_msg);// add success message
    } 
    else
    {
    $("#add_err").html(data.error_msg);// add error message
    }
    
    打赏 评论

相关推荐 更多相似问题