-1

I want to pass data from one page to another page. I tried below code. The result_display.php always gave me an empty array of _POST. I'd appreciate it if someone can point me where is wrong. Thanks.

index.php

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>js save session</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="submit" id="mybutton" value="My Button">


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
    $(document).ready(function () {

        $("#mybutton").click(function () {

            $.ajax({
                method: "POST",
                url: "./result_display.php",
                data: {name: "John", location: "Boston"}
            })
                .done(function (msg) {
                    console.log("Data Saved: " + msg);
                });

            window.location.href = "result_display.php";
        });
    });
</script>
</body>
</html>

result_display.php

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>

<?php
    var_dump($_POST);
?>
Zhongjie Fan
  • 65
  • 2
  • 9
  • Try adding the "traditional" parameter to your ajax call like in one of the answers here: https://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters – agileMike Oct 18 '17 at 21:33
  • Thanks for replying. It's just same. I ran index.php at first. Then refresh result_display.php, still got an empty _POST array. – Zhongjie Fan Oct 18 '17 at 23:37
  • Ahh I misunderstood. The other comments are correct. If you do a POST and then a GET, those are 2 separate requests. The GET won't have the data. If you need those params in the GET then send them in as querystring parameters: window.location.href = "result_display.php?name=John&location=Boston"; And you might not need the POST then at all. – agileMike Oct 19 '17 at 14:20
  • Thank you, agileMike. I appreciate your explanation. – Zhongjie Fan Oct 20 '17 at 03:25

2 Answers2

0

You need to remove the redirect that you are doing after sending the AJAX call.

Remove: window.location.href = "result_display.php";

jacefarm
  • 6,747
  • 6
  • 36
  • 46
aknosis
  • 3,602
  • 20
  • 33
  • Thanks for replying. I removed the line you mentioned and added traditional attribute like agileMike suggested. It's just same. I ran index.php at first. Then refresh result_display.php, still got an empty _POST array. – Zhongjie Fan Oct 18 '17 at 23:39
0

I put redirection back for testing. After I ran index.php and redirect to result_display.php immediately after AJAX call. I found why there is GET after POST. Anyone knows why? Please see attached screenshot. enter image description here

Zhongjie Fan
  • 65
  • 2
  • 9
  • Is that because of the redirection statement? But if I remove redirection statement, how can I check the _POST array in result_display.php ? – Zhongjie Fan Oct 19 '17 at 00:07
  • You can't... when you load that page in your browser, you're doing a GET request. _POST will be blank. If you want to *save* the results of your ajax request for later viewing then you have to save them to a database, and load them by attaching a query parameter like `?id=5` so display_results knows which record to load... – mpen Oct 19 '17 at 00:09
  • You could use a form to post your data to your result page. AJAX is great if you want to send data to your PHP script, run some code on it, return something, and then use that returned data in the current page. You do actually return data and store it in "msg", you write that to the console and then you redirect to your php script again, as mpen mentioned, it uses a GET request that way so there's no data to be read when you redirect. – Ricktron3000 Oct 19 '17 at 00:31