-1

HTML

<div pid="14" class="buybutton">Buy</div>

Javascript

     <script>
       $(document).ready(function() {
           $(".buybutton").click(function(){
               console.log("Clicked Button");
               var pid = $(this).attr("pid");
               console.log(pid,"= Product ID");
               $.post("/redirecttoproduct.php", {"pidofproduct": pid});
            });
       });
      </script>

Console:

Clicked Button
14 = Product ID 

redirecttoproduct.php

<?php
    session_start();
    $_SESSION['redirectproductpid'] = $_POST['pidofproduct'];
?>

Trying to echo SESSION, nothing shows up

<?php
  $productpid = $_SESSION['redirectproductpid'];
  echo $productpid;
?>

Nothing shows up - Any ideas?

Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86

2 Answers2

0

Did you missed the session_start(); in this page? session_start() is necessary when you use $_SESSION.

<?php
  session_start();
  $productpid = $_SESSION['redirectproductpid'];
  echo $productpid;
?>
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Thanks - That's what was missing. This solves the problem partially: I have a plain HTML website and a billing management system called WHMCS in my public_html folder. The "Buy Button" is in the plain html, and I need to check for this session on WHMCS. WHMCS is on the same server, same folder, same everything - How can I do this please? Thanks! – Lucas Bustamante Mar 18 '14 at 03:38
  • PS: It echoes SESSION on plain html, but not on WHMCS – Lucas Bustamante Mar 18 '14 at 03:40
0

I suggest do a callback function first to check if the post request is successfully submitted, one of the reason your $_SESSION variable doesn't return any value is because your js code doesn't post any values to it at all, in result no data is being assigned to your $_SESSION var, ie:

$.post("/redirecttoproduct.php", {"pidofproduct": pid})
.done(function() {alert( "success" );})
.fail(function() {alert( "error" );});

And it would be more convenient if you could supply an absolute path to your $.post request like your website's dir url ie: $.post("//yourwebsite.com/redirecttoproduct.php") with this relative path errors can be significantly avoided, and make sure your not submitting invalid values that would not pass your XSS filters (if there are any), hope this helps, cheers!

  • Thanks, the problem actually is being another - Please come at PHP Chat for more details http://chat.stackoverflow.com/rooms/11/php – Lucas Bustamante Mar 18 '14 at 04:19