2
votes

I have a little problem. I want to pass two variables from PHP to $.ajax success function

I have this code written in JS:

$.ajax({
                        type:"POST",
                        url:path,
                        data: "value="+info,
                        success:function(data,data1)
                        {
                            if(data==1)
                            {
                                $(id).css("backgroundColor","green");
                                $(id).attr("readonly","readonly");
                                $(image).attr("src",data1);
                            }
                            else
                            {
                                $(id).css("backgroundColor","red");
                            }
                        } 
                });

And my PHP code is :

if(isset($_POST['value']) and !empty($_POST['value']))
{
$result=0;
$image="src/photo1.jpg";
$value=trim($_POST['value']);

if($value=="Abracadabra" or strcmp($value,"Abracadabra")==0)
{
$result=1;
$image="src/abracadabra.jpg";
}
else
{
$result=0;
$image="src/photo1.jpg";
}

echo $result;
echo $image;
}

There, I want to "echo" two variables simultaneously to pass to $.ajax success function.

It is possible ? I don't refer to JSON, I refer only PHP with POST method.

Thank you

1
Could you combine them both into one string and split them up later again? - Uwe Keim
What do you mean by "don't refer to JSON"? You can easily return a JSON string from your PHP code instead of 0 or 1... - Elad
I didn't work with JSON anymore. - Teodorescu
That's too bad, JSON would be the cleaner way to go on this exercise/case. - Pedro Serpa

1 Answers

2
votes

the response from php code will be something like:

0src/photo1.jpg

and you will have to parse it with the javascript (probably with regex or substring)

success:function(data,data1) {
   var result = data.substring(0,1);
   var image = data.substring(1);
   // ... your code
}

keep in mind that it could cause you trouble if the $result variable is more than one character long :)