I'm trying to make a simple download counting script that sends data to a MySQL database. I make an associative array which I send through Ajax to a PHP script, however, although I have confirmed that the array contains what I put into it in the JavaScript, the $_POST variable is empty in my PHP.
This is what my JavaScript function looks like:
function download(name) {
var post = {};
post['fileName'] = name;
$.ajax({
url: "phpScripts/downloadCounter.php",
type: 'POST',
data: post
});
window.location = "downloads/" + name;
}
Then, within my PHP script (downloadCounter.php) I have this to check the $_POST variable:
ob_start();
var_dump($_POST);
error_log(ob_get_clean());
error_log($_POST["fileName"]);
The var_dump posts an array of length 0 and the attempt at retrieving the data directly results in an error saying that the index "fileName" is undefined.
The page in question is http://strongjoshua.com/projects/software/line_counter. Upon pressing the download button Chrome reports a POST to the script file with return code 302 (file found), a GET of the actual downloading file, and finally another GET for the script again. This makes it seem like the POST request only reaches as far as finding the file and not actually executing it... Why/how is this happening?
I have done that form of an Ajax request before and it has always worked, so I am at a lost as to why it is not working now. I am using PHP 5.6 on a server hosted by EasyDNS's web hosting.