I am using ajax to retrieve data from my database and store it as an array, then I return the result. My goal is, when the users click the "Click Me" button. I want to alert the first array from the data that is returned. However, my code below is not returning anything.
<input type="button" id="click_me" value="Click Me"/>
var data_array;
$.post('generate.php', {id: id}, function(data){
data_array= data;
});
$('#click_me').click(function(){
alert(data_array[0]);
});
generate.php
<?php
header('Content-Type: application/json');
$array = array('Hello', 'Good Morning', 'Nice to meet you');
echo json_encode($array);
?>