0

I have a function for which validates if the article number inserted isn't in use anywhere on the site. The function posts the number to php where it checks the nubmer and returns true or false accordingly.

This all done using JSON. The data returned by php is a json object from where I take out the response and then return it in the javascript function. I have a console.log(data['response']) to see the response and it is correct.

Now to the problem. I have an if statement where I check if the response is false and if so, outputs an alert. The porblem now is that the if statement never gets fired off as in doesn't show any alert message.

The function in javascript

function validateArticleNr(anr){
    var response = true;
    $.ajax({
        type: 'POST',
        url: '../lib/action.php?action=vanr/validate',
        data: {
            postAnr: anr
        },
        dataType: 'json',
        success: function(data){
            console.log(data['response']);
            response = data['response'];
            return response ;
        },
        error: function(xhr, status, error){
            console.log(xhr.responseText);
        }
    });
}

My php script in action.php

case 'vanr/validate':
        $response = true;
        $anr = $_POST['postAnr'];
        $que = $mysqli->query("SELECT article_nr FROM stock WHERE article_nr='" . $anr . "'");
        $que2 = $mysqli->query("SELECT code FROM sets WHERE code='" . $anr . "'");
        $que3 = $mysqli->query("SELECT article_nr FROM stockpurchases WHERE article_nr='" . $anr . "'");

        while($res = $que->fetch_assoc()){
            if($res['article_nr'] == $anr){
                $response = false;
            }
        }
        while($res2 = $que2->fetch_assoc()){
            if($res2['code'] == $anr){
                $response = false;
            }
        }
        while($res3 = $que3->fetch_assoc()){
            if($res3['article_nr'] == $anr){
                $response = false;
            }
        }

        $return = array('response' => $response);
        echo json_encode($return);
    break;

And the if statement that checks it

if(validateArticleNr(formArticleNr) == false){
        alert('Artikli number on kasutusel');
    }
Karl Viiburg
  • 832
  • 10
  • 34
  • i see no `if` statement ? –  Jan 17 '14 at 03:08
  • you can't do that - you can't return the result of an asynchronous call like this – Arun P Johny Jan 17 '14 at 03:09
  • I accidentaly went against my ENTER key before I could finish writing my question. Edited my question – Karl Viiburg Jan 17 '14 at 03:09
  • So even if I'm returning the response within the ajax success, it won't work as it should? – Karl Viiburg Jan 17 '14 at 03:15
  • @KarlViiburg no it won't... please check the above link.. it has a detailed explanation.... you need to use a callback based solution – Arun P Johny Jan 17 '14 at 03:19
  • @ArunPJohny Many thanks for referring me to this question. This has helped me understand AJAX better and also what asynchronous itself actually means :) – Karl Viiburg Jan 17 '14 at 03:28
  • actually ajax can be synchronous, set async: false. However, it would freeze all actions until server give response, not recommended. In the answer of the the link @Arun provieded, it explained how to do it. No one would recommend it, by the way. – Horst Jan 17 '14 at 03:37

0 Answers0