Edit: Solved by doing what I needed in ajax().done, didn't know that was one of the callbacks available til I looked into this more.
Ok, here is my code. I am trying to get some ids that are filled in with php then use ajax to run a query to see if some related property is set or not. I want to access the responseJSON property, but if I try to access directly it is undefined. However, if I just write the whole variable to console it's all there so I guess I'm missing something. I also tried copying the response to a global variable, but that just gives me the empty value I set in the beginning. How can I access the response?
js
<script>
var valid;
$(document).ready(function(){
//get all appointment numbers
count=0;
appointment_nums = [];
$('.mgrid_table > tbody > tr').each(function() {
var temp = ($(this).find('td').eq(3).find('label').html());
appointment_nums.push(temp);
});
appointment_nums = appointment_nums.filter(function(n){ return n != undefined });
appointments = appointment_nums.length;
scheduled = $.ajax({
type:"post",
url: "../testrequest.php",
data : {appointment_nums:appointment_nums},
dataType:'json',
done:function(response){
valid=response;
}
})
console.log(valid);
console.log(scheduled);
$('table:nth-of-type(2) > tbody > tr > td:nth-of-type(2)').each(function() {
if($(this).children().length < 1){
$(this).append('<a href="index.php?doctor=test&a='+appointment_nums[count]+'">Schedule Appointment </a>');
}
count = count + 1 ;
});
});
</script>
php
$appointment_numbers = (isset($_POST['appointment_nums'])) ? $_POST['appointment_nums']:0;
if ($appointment_numbers!=0){
$appointments_scheduled =array();
foreach ($appointment_numbers as $apt_num){
$sql = 'SELECT event_id FROM '.TABLE_APPOINTMENTS_SCHEDULING.' WHERE appointment_number = "'.$apt_num.'"';
$res = database_query($sql);
array_push($appointments_scheduled, empty($res));
}
Edit: I don't think this is a duplicate, it's a slightly different way of getting that same info but my issue is not the same. If I do console.log(scheduled) anywhere, I get the response object with everything set. So if I see the data, it must be there and it's showing after the response is made. But then I can't access the object, I'll put a picture to show what I mean. Also, valid and scheduled are two attempts to get the same info. The other posts don't help with the way I am trying to do it, I've checked.