0

I want to use POST variables with AJAX to post them to a page called results.php:

$.ajax({
    type: "POST",
    url: "results.php",
    data:"score="+score+"&round="+round,
    success: function(data) {
      window.location.href = 'results.php';
    }
  })

The code is working and directing to results.php, but the page on results.php can't read the data for score or round. I know the data is correct, but the data isn't transfering?

Cøde Play
  • 157
  • 8
  • 1
    In that case, why would you not just set up a form with the action set to `results.php` and have the user click on submit? The way your code works right now, is that when the data is sent successfully, your AJAX doesn't retrieve the results, but redirect the user to results.php, not carrying the data variables. –  Apr 29 '16 at 03:38
  • Possible duplicate of [JavaScript post request like a form submit](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) – Jacob See Apr 29 '16 at 03:38
  • @JacobSee This question is very close. I don't want to use a form, only AJAX. Is this possible? – Cøde Play Apr 29 '16 at 03:42
  • @LFlare is correct. You should just use something like `
    `
    – Cave Johnson Apr 29 '16 at 03:44
  • 1
    @CødePlay Not with pure AJAX. You're going to need a form in some way, shape, or form (heh) for this one. – Jacob See Apr 29 '16 at 03:46
  • 1
    You can still make a form but set visibility or display to none **and** then use javascript to submit the form. Still redirects, still works without user interaction :) –  Apr 29 '16 at 03:47

2 Answers2

1

To incorporate what has been said in comments:

AJAX sends this request in the background (the fundamental purpose of AJAX). You then change the location.href to results.php which is actually a different request and does not have the parameters. It sounds like what you want is a form that POSTs to results.php, which will automatically redirect the browser to that page.

Superfly
  • 433
  • 3
  • 12
  • It still wouldn't work, OP's sending the data on a one-way journey to results.php **and** then loading results.php without the data variables. He needs to set it up like a form –  Apr 29 '16 at 03:40
  • The data value CAN be formatted like that and still be POST if the type is set as POST. See the [documentation](https://api.jquery.com/jquery.ajax/). It is converted to a query string whichever way you format it. – Cave Johnson Apr 29 '16 at 03:42
  • Thanks to LFlare, Andrew and Superly for posting answers concerning forms. I added a value to the input, then submitted the form with Jquery. I'm accepted your answer because you're the only one that posted an answer of the three... – Cøde Play Apr 29 '16 at 03:57
0

This would not be work because you first send the data to the result.php page and after that you redirecting to the result.php page.

There is a JQuery plug-in that accomplishes pretty much what you're trying to do: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js.

After including JQuery and the jquery.redirect.min.js plug-in, you can simply do something like this:

Try like this.

$().redirect('result.php', {'score': 'score', 'round': 'round'});

Happy Coding!!!

Kalpesh Rajai
  • 1,852
  • 23
  • 33
  • Download that `js` file and host at your server and after that use `Script` tag to add the`js` to your page and use that `statement` to redirect to the page – Kalpesh Rajai Apr 29 '16 at 03:49
  • When you want to redirect to the `result.php` page ? If at the button click event than use the form with the `method=post` and `action=result.php`. It is simple and work more efficiently. – Kalpesh Rajai Apr 29 '16 at 03:51
  • This works if you change it to: `$.redirect('result.php', {score:score, round:round});` – bagofmilk Apr 04 '17 at 04:09