0

I have a page, return.php. It has a line of HTML as follows:

<td><a href="#" dataP="<?= $partNumber[$x] ?>" onclick="popup(this)"><?= substr($avgCost[0],0,6) ?></a></td>

onclick, this calls the following popup function:

function popup(windowname) {  
  document.cookie = 'popupPartNumber='+windowname.getAttribute("dataP")+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
  var x = document.cookie;
  console.log(x);    
  windowname = 'popUpDiv';
  blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);

}

on this same page, this loads:

<div id="blanket" style="display:none;"></div>
  <div id="popUpDiv" style="display:none;"">
  <a href="#"" onclick="popup(this)"><img src="/img/close.png"></a>
<?php
  $popupPartNumber  = $_COOKIE['popupPartNumber'];
  echo "<pre>";
  echo htmlentities($_COOKIE['popupPartNumber'], 3, 'UTF-8');
  echo "</pre>";
?>

</div>

When I start this out and I click on the HTML link, I get a console.log output that says:

PHPSESSID=cvbt1gvisul27nccenaeum1hh0; popupPartNumber=123456789

but my php echo output says: null

Why is the cookie that I'm setting correctly in javascript not available to php?

Update

I changed return.php to say:

$popupPartNumber = $_POST['popupPartNumber']; //part number line
  echo "<pre>"; 
  echo "value is: " . $popupPartNumber ;
  echo "</pre>";

and I changed the script to read:

var x = windowname.getAttribute("dataP");

  $.ajax({
      type: 'POST',
      url: 'return.php',
      data: {popupPartNumber:x},        
        success:function(data){
          alert(data);
          }
        });

console.log(x); //outputs the correct number.

This isn't working though... I still get no value to the php variable.

Brian Powell
  • 3,336
  • 4
  • 34
  • 60
  • possible duplicate of [Set cookie wih JS, read with PHP problem](http://stackoverflow.com/questions/5045053/set-cookie-wih-js-read-with-php-problem) – Matjaž Feb 04 '15 at 19:14
  • Are you on the same domain? – Alex Shilman Feb 04 '15 at 19:15
  • @MatjažMav interesting, that's the exact thread I'm reading right now. Where in this thread do you see an answer to my question? Cause I don't. – Brian Powell Feb 04 '15 at 19:15
  • @AlexShilman yes - all of this code is on the same page. – Brian Powell Feb 04 '15 at 19:15
  • Looks like the JavaScript code **sets** the cookie too, which means that when the page first loads (that is, when the PHP code runs) the cookie might not be set yet. – Pointy Feb 04 '15 at 19:17

1 Answers1

0

Your php code generates html code on the server, reads cookies, and passes html to the browser. So those cookies don't exist at that time on the server. On the browser you are setting your cookies. So after you set those cookies on the browser do an ajax call to the server and you should be able to read those cookies on the server at that time.

Alex Shilman
  • 1,547
  • 1
  • 11
  • 15
  • Beautiful. That's kind of what I was gathering.... let me put it into an AJAX call instead. Thank you! – Brian Powell Feb 04 '15 at 20:13
  • I'm having a hard time with this - can you take a look at my original question and see where I've gone wrong? – Brian Powell Feb 04 '15 at 20:37
  • I thought you wanted to read is as a cookie: echo $_COOKIE['popupPartNumber'] – Alex Shilman Feb 04 '15 at 20:40
  • yeah my bad - I was testing out a few different ways to "get" it back and none of them worked... I tried initially with `$popupPartNumber = $_COOKIE['popupPartNumber'];` but that's null too. Is there something wrong with my AJAX code? – Brian Powell Feb 04 '15 at 20:48