var bbb;
var ma_category;
ma_category="Vocabulary";
$(document).ready(function() {
$("#create_matching_activity").click(function(){
$.ajax({
type: "POST",
url: "a_category_code.php?ma_category="+ma_category,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
bbb=data.Act_Category_Code;
},
complete: function(){
}
});
alert(bbb);
});
});
<?php
include('ActivityConf.php');
$ma_cat=$_GET['ma_category'];
$sql = $connect->prepare("SELECT Act_Category_Code FROM activity_category WHERE Act_Category='".$ma_category."'");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute();
$row = $sql->fetch();
$connect=null;
header('Content-type: application/json');
echo json_encode($row);
?>
This code reads from a database. im passing a variable from javascript to php using ajax. all is fine, but when i click to get my answer i get"undefined" as alert for first click, then the second click on i get the right variable in the alert.
what is the reason of this delay? Please if anyone can assist this issue. thank you.