Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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"   
    
    }

presave.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

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

$.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.

[Edit]- I have added some code for your better understanding. You can refer to this excellent "JQuery Ajax POST Method" tutorial from freecodecamp.org. https://www.freecodecamp.org/news/jquery-ajax-post-method/

game.php

<?php
session_start();    
?>

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <button id="btn" onclick="save();">Click this</button>

  <script type="text/javascript">
  var drzewo = 0;

  function save() {
    $.post("save.php", {
        drzewo: drzewo
      },
      function(result) {
        alert("This result contains the output from save.php : " + result);
        //Write here the code you want to execute after the POST request is successfully completed.
      }
    );
  }
  </script>
</body>

</html>

save.php

<?php
  session_start();

  $_SESSION['drewno'] = $_POST["drzewo"];
  echo $_SESSION['drewno'];
about 4 years ago · Juan Pablo Isaza Denunciar

0

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']
?>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda