1

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.

1

3 Answers 3

5

i just changed the following

document.cookie='fcookie='+tempo; 

and

if (isset($_COOKIE["fcookie"])) 
echo $_COOKIE["fcookie"]; 
else 
echo "Cookie Not Set";
Sign up to request clarification or add additional context in comments.

Comments

3

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

5 Comments

both of my scripts are on different files, it gives me a Notice: Undefined index: mycookie
@PatrickNarcelles I don't think that will a issue, because if a cookie is set you can access it from different file.
@PatrickNarcelles that's because your cookie was not set, check this page javascript.about.com/library/blwcookie.htm , it explains creating cookie setting domain path and cookie expiry date, also i have edited my answer.
now it goes to the else part, it displays Cookie Not Set, i tried your suggestion, which is to add fcookie='mycookie';
thanks for the tips sir, i just changed the following document.cookie='fcookie='+tempo; and if (isset($_COOKIE["fcookie"])) echo $_COOKIE["fcookie"]; else echo "Cookie Not Set";
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.