Skip to content Skip to sidebar Skip to footer

Set Cookie Value In Javascript And Displaying It With Php

i am having an output problem and i can't seem to trace the problem, here is the code: sample.js var m_names = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Ju

Solution 1:

i just changed the following

document.cookie='fcookie='+tempo; 

and

if (isset($_COOKIE["fcookie"])) 
echo $_COOKIE["fcookie"]; 
else 
echo "Cookie Not Set";

Solution 2:

Your script has couple of mistakes, I have modified them and added some extra codes, Hope this works for you

<script>
    fcookie='mycookie';
    var monthname = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var myDate=new Date();//--->getting today's date
    var cmonth = myDate.getMonth();
    var cdate = myDate.getDate();
    var temp1 = monthname[cmonth];
    var tempo = escape(temp1 + " " + cdate); 
    document.cookie=fcookie+"=" + tempo;//-->missing cookie name and concatenation
    </script>
    <?php
    if (isset($_COOKIE["mycookie"]))
      echo $_COOKIE["mycookie"];
    else
       echo "Cookie Not Set";
    ?>

More about Javscript cookies and Php Cookies


Solution 3:

First of all, the $_COOKIE you are seeing is the PHPSESSID cookie... You are not viewing the JS cookies. This article has good info on the relationship between PHP and JS cookies: http://www.quirksmode.org/js/cookies.html


Post a Comment for "Set Cookie Value In Javascript And Displaying It With Php"