Patrick Narcelles
Patrick Narcelles

Reputation: 167

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", "July", "August", "September", "October", "November", "December");
var cmonth = myDate.getMonth();
var cdate = myDate.getDate();
var temp1 = m_names[cmonth];
var tempo = escape(temp1 + " " + cdate);
document.cookie=fcookie"=" + tempo;

output.php

<?php echo implode($_COOKIE)?>

and it displays

713qnihjmdt7mdq8eejvlcd1q1

but i want to display the date stored in the tempo variable,

i tried dispaying the tempo variabe directly and it dispalyed the right output,

any suggestions? i think i need to add a code in the php side.

Upvotes: 1

Views: 18512

Answers (3)

Patrick Narcelles
Patrick Narcelles

Reputation: 167

i just changed the following

document.cookie='fcookie='+tempo; 

and

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

Upvotes: 5

Sibu
Sibu

Reputation: 4617

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

Upvotes: 3

christopher
christopher

Reputation: 643

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

Upvotes: 1

Related Questions