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');
}