0

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);

lch
  • 27
  • 8
  • 1
    You need `json` datatype to do that. Take a look at [this](http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc) and also search on this matter. – machineaddict Aug 06 '14 at 13:46

2 Answers2

1

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
}
Harshal
  • 3,562
  • 9
  • 36
  • 65
  • What do you mean pass the flag of success:1? Do I set $success = '1'? – lch Aug 06 '14 at 14:10
  • yes some thing like this , if code runs successful than pass 1 otherwise 0 and handle it at client side. – Harshal Aug 06 '14 at 14:15
  • As in how do I set it ? Cause now currently I set the $success = the success message. – lch Aug 06 '14 at 14:23
  • than you can set the error message on error, so you can check with EMPTY check to check which message is coming from server, see Updated part of my answer. – Harshal Aug 06 '14 at 14:31
1

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.

FunkyMonk91
  • 1,469
  • 1
  • 17
  • 30
  • 1
    Please add an `exit` or `die` after `json_encode` or you could also return more then json. – machineaddict Aug 06 '14 at 14:01
  • Erm i tried this but its not showing me the messages – lch Aug 06 '14 at 14:29
  • It only shows me the success message but not the error message – lch Aug 06 '14 at 14:33
  • If there is no error message, data.error will be blank. If you are forcing an error for testing, ensure that you define `$error` before giving it a value. If the `$error` in the json_encoded array isnt defined it may give unexpected results – FunkyMonk91 Aug 06 '14 at 14:34
  • I defined the $error already and i purposely made an error but the message is not showing – lch Aug 06 '14 at 14:37
  • Is working now. I found out that I did not define correctly! Thanks ! – lch Aug 06 '14 at 14:43