if($rx==$_SESSION['randomx'] and $ry==$_SESSION['randomy']){
echo "Cestitam, zadeli ste pravilno celico! rabili ste samo:".$_SESSION['poskus'];
}
else{
$razdalija=sqrt(($rx-$_SESSION['randomx'])*($rx-$_SESSION['randomx'])+($ry-$_SESSION['randomy'])*($ry-$_SESSION['randomy']));
echo $_SESSION["poskus"].". Zgresili ste za: ".round($razdalija);
$_SESSION["poskus"]++;
}
Both echos return a sentense how can i differenciete those two sentences? In the ajax function i want to compare which one came back so i can set the background color.
-
Instead of returning a string, return structured data like JSON or XML containing meta data on which peace of data was returned.Musa– Musa2015年01月22日 22:39:54 +00:00Commented Jan 22, 2015 at 22:39
1 Answer 1
I would return json instead and use the key to differentiate between the possible outputs.
For example:
$arr = array();
if ($rx==$_SESSION['randomx'] and $ry==$_SESSION['randomy']) {
$arr['good'] = "Cestitam, zadeli ste pravilno celico! rabili ste samo:".$_SESSION['poskus'];
} else {
$razdalija=sqrt(($rx-$_SESSION['randomx'])*($rx-$_SESSION['randomx'])+($ry-$_SESSION['randomy'])*($ry-$_SESSION['randomy']));
$arr['bad'] = $_SESSION["poskus"].". Zgresili ste za: ".round($razdalija);
$_SESSION["poskus"]++;
}
echo json_encode($arr);
Now you can check in javascript which one is set and do what you want to do.
You could also return an additional value that determines the status and a text value for the text, plenty of possibilities. The key is sending back structured data instead of just a text string.
answered Jan 22, 2015 at 22:40
jeroen
91.8k22 gold badges118 silver badges132 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
user3617642
And then i can use if(data=='bad') and so on?
jeroen
@user3617642 Not directly, if the json is parsed, you'd use something like
if ('bad' in data) or if (data.hasOwnProperty('bad')). And then you use data.bad or data.good which contains the text.user3617642
The numbers are undefined, i must be doing something wrong.
jeroen
@user3617642 What numbers and is the json parsed? You'd need to ask a new question or post the ajax function to get help with this specific problem.
default