简体   繁体   中英

Want to retrieve a cookie value

my program generate a cookie with this code :

    echo "
    <script>
    $('td').click(function(){
        document.cookie=$(this).text();
        alert(document.cookie);
    });
    </script>
    ";

It works fine, document.cookie is the same as the td value. However i want to call it on a php function later such as :

function fill_input() {
    $value = $COOKIE["document.cookie"]; 
    echo $value;

But it said that it is not set. Please help me i am a student and i already spent one full day trying to figure this simple problem. Thanks you have a nice day.

You need to access your cookie by it's name, right now you're passing a string document.cookie .

So, whatever $(this).text() is, will be how you access the cookie. If it returns blablabla , then

function fill_input() {
    $value = $_COOKIE["blablabla"]; 
    echo $value;

Using document.cookie you need to set a name and a value. The way you do it the name is empty you only set a value.

  echo "
    <script>
    $('td').click(function(){
        document.cookie='myCookie='+$(this).text();
        alert(document.cookie);
    });
    </script>
    ";

Above code set the name and the value to the cookie and the you can call it like:

$_COOKIE["myCookie"]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM