This question already has an answer here:
I'm trying to pass an array of latitude values into my PHP file so I can store them in a database. However nothing I've tried works. When trying to assign the sent array to a php array I get "undefined index".
The array of latitudes gets filled with values every 10 seconds once a button is pressed and stops when a stop button is pressed.
JavaScript
lat.push(position.coords.latitude);
console.log(lat); // lat array definitely isnt empty
function stopJourney() // stops timer, called by button press
{
clearInterval(timer);
document.getElementById("journeyStart").disabled = false;
$.ajax({
type: 'POST',
url: 'Functions.php',
data: { 'latArray' : JSON.stringify(lat)},
dataType: 'json'
});
window.location.replace("other page that isn't relevant");
}
my PHP
$_SESSION['latArray'] = json_decode($_POST['latArray']);
Gets this error
Notice: Undefined index: latArray in /.../.../.../.../PHP/Functions.php on line 293 // full file location not relevant
The lat array is accessible in the function
function stopJourney() // stops timer
{
clearInterval(timer);
document.getElementById("journeyStart").disabled = false;
var stringLat = JSON.stringify(lat);
alert(stringLat);
//$.ajax({
// type: 'POST',
// url: 'Functions.php',
// data: { 'latArray' : stringLat},
// dataType: 'json'
//});
//window.location.replace("other page that isn't relevant");
}
var_dump($_POST)
Shows
array(1) {
["latArray"]=>
string(20) "[53.732015499999996]"
}
</div>