user3796133
user3796133

Reputation: 348

$_GET variable not staying set when set with ajax?

sorry i am new to ajax and well programming in general.

I have a set of divs each containing a set of products for an e-comm site. Only one div is visible at a time when a certain stock button is clicked.

when a user clicks on a product displayed inside the visible div the user is taken to a new page where the product details are displayed. Basically What i'm trying to do is have a history when the user clicks the back button in the browser, i want it to return to the last div that was visible when the page is loaded.

I am trying to use ajax to send a variable to a php file when the user opens a product div, and then retrieve that variable when the user clacks back and the page reloads. I can send the variable fine with ajax to the php file and return with the success property of the same ajax function. But after that, when i try to retrieve the variable, it appears as unset.

Ajax send method:

    // when the user clicks the apparel stock button the corresponding div becomes visible 
    $("#stockApparel").click(function(){

      var $apparel = "apparel";

      $.ajax({

          url: "php/panel.php",

          data: { panel: $apparel },

          type: "get",

          dataType: "text",

          success: function(response){
              alert(response); // response is "apparel" as intended/
          },

      });


      $("#stockButtons").animate({left: '1140px'});
      $("#new").animate({left: '-470px'});
      $("#apparel").show(700);
      $("#marketing").hide(100);

      var divPosition = $('#shippingImg').offset();
      $('html, body').animate({scrollTop: divPosition.top}, "slow");

  });

Get method to retrieve variable

$(window).load(function(){
         alert('window has been fully loaded');

        $.get("php/panel.php", function(data, status){
            alert("Data: " + data + "\nStatus: " + status); // returns as "variable not set".
        });

     });

Php file

if (isset($_GET['panel'])) { // check if varaiable has been set

    $panel  = $_GET['panel'];
    switch ($panel) {
        case "collectable":
            echo "collectable";
        break;

       case "apparel":
           echo "apparel";
       break;

       default:
    }
 } else echo "Variable not set."; // if variable is not set

?>

even if i put the $.get method directly after the ajax send method i still get the "Variable not set" returned from the php file, any ideas what im doing wrong?

Upvotes: 2

Views: 301

Answers (3)

wbars
wbars

Reputation: 533

Answering for this question straightly: no, get parameters don't stay on server-side between requests.
However, you might want to read some basics of client-server interactions.
In two words: in first case you send GET request with some parameters
panel.php?panel=value
in second case - it is just plain GET request without it. Server does not keep information about previous requests himself and does not have to. If you wanna keep this information, you might want to use some sort of database logic, or session/cookie caching (there is a answers, that provide code examples of this).

Upvotes: 1

Riad
Riad

Reputation: 3850

Set the value of panel:

$.get("php/panel.php",{panel:'apparel'}, function(data, status){
            alert("Data: " + data + "\nStatus: " + status); // should get the panel variable now..
});

Upvotes: 0

Shyju
Shyju

Reputation: 218722

Http is stateless. You need to temporarily persist that information so that you can return it when user request it again (page reload).

You may use session variable to persist it and read it from that and return it in your server page.

Something like this, (I am not a PHP person, But you get the idea ?)

<?php
session_start();
if (!isset($_SESSION['panel'])) {
  // to do : check $_GET['panel'] before setting to session
  $_SESSION['panel'] = $_GET['panel'];
} 
return $_SESSION['panel'];

?>

Upvotes: 0

Related Questions