简体   繁体   中英

How to get "$.Post" variable in another site

i search internet for my question and i doesn't found how to get my $_POST on another page. Please help.

game.php:

<?php
    session_start();    
    ?>
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 <script type="text/javascript">

    var drzewo = 0;

function save() {
    
        $.post( "save.php", { drzewo: drzewo } );
        window.location.href = "save.php"   
    
    }

save.php

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

<?php
    session_start();

        $_SESSION['drewno'] = $_POST["drzewo"];
echo $_SESSION['drewno']

?>

Undefined array key picture

Your question seems incomplete in my opinion. There are many things missing from your question. From your image I assume you are using it on local server. There are one thing that looks weird is this:

`$.post( "save.php", { drzewo: drzewo } );`

Where is this save.php file stored? Is it stored in your root file? Or along with the operating file. Put a "/" before save.php and see what happens. There is another thing that seems awkward is your image is showing there is a presave.php file. But I found nothing about that file in your question details. I can see that you put a redirecting code in your script. JS is not supposed to work like that. What happening here is it is doing an asynchronous operation. That is why PHP can't get the data and showing you a error. If you want to send data send it in a form. Or you can put a redirection in your PHP code after getting the post data. Since it is an AJAX call you can see some reports in your console.log() with the response data.

$.post( "save.php", { drzewo: drzewo } );
window.location.href = "save.php"   

Let's analyze these 2 lines of code. When you send a post request through javascript, it goes through an ajax request and the browser waits till the save.php completes working it's code and returns to the ajax. Here the save.php has received the $_POST data and has processed it.

This processed data by save.php will not output in the browser. It will go to the post (ajax) callback function. If you want to display the data, you need to use a callback function. See this https://www.w3schools.com/jquery/ajax_post.asp for a simple usage of $.post() function.

In the next line of your code you are redirecting your browser to save.php without sending any $_POST data. Since now your save.php does not have any information about $_POST["drzewo"] it throws an error Undefined array key "drzewo" .

Therefore instead of window.location.href page redirect, you need a callback function with your $.post() function. I hope now you understand why you are getting this error.

If you want to pass data from one URL to another, use query string. $.post() makes a POST request, but it seems like save.php is not an endpoint but actually a page.

In other words, you can do this:

window.location.href = `save.php?drzewo=${drzewo}`;

And then on save.php simply read the value from $_GET :

<?php
    session_start();

    $_SESSION['drewno'] = $_GET["drzewo"];
    echo $_SESSION['drewno']
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM