I am trying to pass an 2D array from JavaScript to PHP using Ajax. The Ajax said it is success but after redirect to PHP page, but no data is set in the PHP page. I have tried two methods but both of them cannot successfully post the array.
The array looks like:
num = [["StringA",0], ["StringB",1], ["StringC",2]];
This is the first method I tried in JavaScript:
$.ajax({
type: "POST",
url: "database.php",
contentType: 'application/json',
processData: false,
data: { array : JSON.stringify(num) },
success: function()
{
window.location = "database.php";
}
});
I have also tried this:
$.post("database.php", {array: num})
.done(function(data){
window.location = "database.php";
});
In database.php:
<?php
if (isset($_POST["array"])){
echo "Data is set";
print_r($array);
}
?>
Both of them can successfully redirect to database.php, but none of them show something on screen.
I am not familiar with Ajax. How can I post an array to the php page right after the array is sent?