2012-09-12 93 views
1

我有一個輸出的問題,我似乎無法追查問題顯示出來,這裏是代碼:設置cookie的值,並用PHP

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; 

輸出.PHP

<?php echo implode($_COOKIE)?> 

並顯示

713qnihjmdt7mdq8eejvlcd1q1

,但我想顯示存儲在節奏變化之日起,

我試圖直接dispaying節奏variabe並dispalyed正確的輸出,

有什麼建議?我想我需要在PHP端添加一個代碼。

+1

相關:http://stackoverflow.com/questions/5045053/set-cookie-wih-js-read-with-php-problem – jedwards

回答

2

我只是改變了以下

document.cookie='fcookie='+tempo; 

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

你的腳本夫婦的錯誤,我已經修改了他們,並增加了一些額外的代碼,希望這對你的作品

<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"; 
    ?> 

更多Javscript cookiesPhp Cookies

+0

我的兩個腳本都在不同的文件,它給了我一個通知:未定義索引:mycookie –

+0

@PatrickNarcelles我不認爲這會有問題,因爲如果設置了cookie,您可以從不同的文件訪問它。 – Sibu

+0

我編輯我的評論sir –