1

hey there i have this script´s:

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    dataType: "json",
    data: 'username=' + $(this).data('id'),
    success: function(data) {
            if (result == 1) {
                    $("#select-err").text(data.error ? data.error : "");
            }
            else {
                    $("#select-err").text(data.error ? data.error : "");
            }
    }
});

in checkAvailability.php:

$availabilityChecker = new AvailabilityChecker($config);

if($availabilityChecker->check_availability($_POST['username'])) {
    echo json_encode(array("error" => "is ok"));
    $result = 1;
} else {
    echo json_encode(array("error" => "Wrong chose"));
    $result = 0;
} 

while testing i found out that this is not the correct way to check if a php-clause is true or false, so i need your help...could anyone show me how to check this via jquery? greetings and thanks!

UPDATE: i changed to:

$availabilityChecker = new AvailabilityChecker($config);

if($availabilityChecker->check_availability($_POST['username'])) {
    echo 1;
} else {
    echo 0;
}

and:

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    dataType: "json",
    data: 'username=' + $(this).data('id'),
    success: function(data){
         if(data == 1){
             $("#select-err").text(data.error ? data.error : "is ok");
         }
         else{
             $("#select-err").text(data.error ? data.error : "not ok");
         }
    }
});

it works, BUT: if data == 1, on my page "1" is displayed, why and how can i fix this?

4
  • And what is wrong with the code you have, why do you think it's not the correct way to do this? There are some things that could be shortened down a bit, but the code in general seems fine to me ! Commented Nov 24, 2013 at 14:20
  • "while testing i found out that this is not the correct way to check if a php-clause is true or false" Commented Nov 24, 2013 at 14:24
  • And why not, why do you assume it's wrong? A if/else condition is just fine, so is echoing JSON when the ajax call has a datatype of JSON, but if you mean the class AvailabilityChecker, we have no idea what that looks like and can't help you with that. Commented Nov 24, 2013 at 14:26
  • 1
    Here's how to do it, add the result to the array you're encoding in the PHP, like this -> echo json_encode(array("error" => "is ok", "result" => 1));, and then in the ajax call check it with if (data.result == 1) {... Commented Nov 24, 2013 at 14:32

3 Answers 3

3

Instead of doing this

if (result == 1) {

do this

if (data.result == 1) {

inside your success callback javascript file.

Then in your PHP file instead of these:

echo json_encode(array("error" => "is ok"));
echo json_encode(array("error" => "Wrong chose"));

do these instead:

echo json_encode(array("error" => "is ok", "result"=>1));
echo json_encode(array("error" => "Wrong chose", "result"=>0));

What I did is I included result as a property in the JSON coming from AJAX call. So instead of only having the error property you also have the result property in the JSON.

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

Comments

2

in php change to this

$availabilityChecker = new AvailabilityChecker($config);

if($availabilityChecker->check_availability($_POST['username'])) {
  echo json_encode(array("error" => "is ok" , "result"=>1));

} else {

  echo json_encode(array("error" => "Wrong chose" , "result"=>0));
} 

and in jquery

check as

if(data.result==1){
  // do the same
}else{
}

1 Comment

but i need to check because of following functions, so could you help me to check if php-clause is true OR false?
0

Don't echo json_encode(array("error" => "is ok")); in php in if-else statement, just echo result in both cases. In you ajax it on success callback, it will return everything that is on ur php page i.e. result which may be 1 or 0. SO check if data==1 or data==0 instead of result.

12 Comments

i am not really sure what you mean? could just show an example? greetings!
i mean just use echo "1" or "0" in php and according to that modify $("#select-err").txt();
but i am using json as datatype, so how to use echo?
@user2999787 The $result=… line is absolutely pointless in the PHP script. It doesn't get returned anywhere. The JavaScript won't see it.
When you're using an ajax call that expects json to be returned, you'll fail miserably when returning just a boolean
|

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.