0
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.

1
  • Instead of returning a string, return structured data like JSON or XML containing meta data on which peace of data was returned. Commented Jan 22, 2015 at 22:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And then i can use if(data=='bad') and so on?
@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.
The numbers are undefined, i must be doing something wrong.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.