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"]; 
elseecho"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 = newArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var myDate=newDate();//--->getting today's datevar 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><?phpif (isset($_COOKIE["mycookie"]))
      echo$_COOKIE["mycookie"];
    elseecho"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"