1

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.

13
  • Check the browser developer tools to see what the outgoing HTTP request looks like. Commented Mar 24, 2015 at 23:12
  • Well starting an XHR right before you reload the page is not really a safe bet; the browser isn't going to wait for the XHR to complete, but the server should get it. You should be able to tell what the POST parameters look like in the HTTP request information from the developer console, and if you have server-side logging you can see what's received there. Commented Mar 24, 2015 at 23:34
  • But that doesn't reload the page? It only downloads the file. Or am I misunderstanding this? The page does not change (although I do understand that I am technically redirecting), only the file is downloaded. If this is the error, then how else would I have the client download something (idk if I want the success function to be the download in case their is a database error, I still want the user to be able to download)? Commented Mar 25, 2015 at 0:00
  • Oh oh I see - your "download" link sets the Content-Disposition header, right? OK, never mind then. In any case, I'd still look at the HTTP request information to confirm that your outgoing HTTP request contains what you think it does. Commented Mar 25, 2015 at 0:10
  • @Pointy I don't even know what a Content-Disposition header is :3 But the POST header that I described contains the actual array, but of course, the GET request does not. Commented Mar 25, 2015 at 0:15

2 Answers 2

1

Your server is set up to automatically redirect a request for
http://strongjoshua.com/phpScripts/downloadCounter.php
to
http://strongjoshua.com/phpScripts/downloadCounter

Now, because the browser uses method GET to follow such redirects, when your script is finally “called”, there is no POST data any more.

Simply change

$.ajax({
    url: "phpScripts/downloadCounter.php",

to use the “correct” address in the first place,

$.ajax({
    url: "phpScripts/downloadCounter",

– that way, the server will not need to redirect to the “final” address first, the request method that your script is called with stays POST – and you get your data in $_POST as desired.

Sign up to request clarification or add additional context in comments.

Comments

0

Javascript

<script>
function download(name) {

var postData = 
            {
                "fileName":name
            }
$.ajax({
    url: "phpScripts/downloadCounter.php",
    type: 'POST',
    data: {myData:postData},
    success: function(output_string){ 
        alert(output_string);
    }
});

//window.location = "downloads/" + name;
}
download('Counter Strike');

</script>

PHP

 <?php
 echo $_POST['myData']['fileName'];
 ?>

Running that script should alert "Counter Strike"

3 Comments

I don't understand how this is different from what I did. All you did was put the post array within another array and pass that. Why would that make a difference? It didn't btw.
I tested it, that's why I posted. @StrongJoshua
You commented out the actual download. And I think that's the problem. Check my last comment on my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.