I have a PHP file which is querying a MySQL database in order to return results, I'm currently saving the relevant result row into a PHP array and I'm then trying to echo out a JS array with the PHP data stored. I do not seem to be able to access the JS variable from my JS files.
PHP:
while($row = mysqli_fetch_array($query)) {
$dataArray = array($row['stepNumber']);
echo '<script>';
echo 'var dataArray = ' . json_encode($dataArray) . ';';
echo '</script>';
}
JS:
$.ajax({
type: 'POST',
url: 'queries/dateRangeSelect.php',
dataType: 'text',
data: {startDate: startDate, endDate: endDate},
cache: false,
success: function(response) {
console.log(response);
window.alert(dataArray);
},
dataArray
is being reported as not defined when the window.alert
tries to trigger.
EDIT:
while($row = mysqli_fetch_array($query)) {
$dataArray = array($row['stepNumber']);
}
header('Content-Type: application/json');
echo $dataArray;
It is no longer printing errors but the console is now not printing anything.