how can I make sure that my AJAX post ($.ajax) runs (sends a variable to my PHP file, and then AFTER that it runs the PHP file?
HTML/JS:
<script>
$(document).ready(function() {
$("#savebutton").click(function(e) {
var jsonString = localStorage.getItem("jsonData");
console.log(jsonString)
$.ajax({
type: "POST",
url: 'savefile.php',
async: false,
data: {
jsondata: jsonString,
name: 'test',
"test": "test2.."
},
success: function(data) {
alert(data);
},
fail: function() {
alert("error")
}
});
})
})
</script>
PHP:
<?php
header('Content-type: text/plain');
header('Content-disposition: attachment; filename="cookieclicker-save.txt"');
print_r($_POST);
?>
HTML Input Button:
<form method="post" action="savefile.php">
<input class="buttonred" id="savebutton" type="submit" name="submit" value="Backup Savefile" style="float:left">
</form>
A few notes: The data
variable in the alert(data)
function, shows the exact response I'm expecting, however, the file that is being downloaded in the PHP file, shows an empty array. I'm guessing this is because the PHP file is being ran before the AJAX call. Could someone help me out here? I really need that Javascript variable in my PHP file. Thanks!