passing javascript cookies into php script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart

    #1

    passing javascript cookies into php script

    Hello,

    I created a php script in which I call php fumcion which looks like
    this

    function confirm($vl) {


    ?>
    <head>
    <script type='text/javascript'>
    <!--
    document.cookie = "qrv=''";
    alert(document. cookie);

    var answer = confirm("ARE YOU SURE?")



    if (answer){
    alert('ok');
    document.cookie = "qrv=yes";
    alert(document. cookie);

    }
    else{
    alert('no');
    document.cookie = "qrv=no";
    alert(document. cookie);

    }

    //-->
    </script>
    </head>
    <?
    }


    Later after I call the above function I want to check what is the
    session value

    echo $_COOKIE["qrv"];

    it is null, but the actual cookie in the browser is set to yes or no.
    When I try again the echo command give me the right output.

    Problem is that I always have to refresh the webpage in order to get
    the cookie value.
    It looks like php reads the cookie that was set before you load the
    browser page.



    Any ideas how to fix that?

    Thank You

    Bartosz Wegrzyn

  • Rik

    #2
    Re: passing javascript cookies into php script

    Bart wrote:[color=blue]
    > <script type='text/javascript'>
    > <!--
    > document.cookie = "qrv=''";
    > alert(document. cookie);
    > //-->
    > </script>[/color]
    [color=blue]
    > Later after I call the above function I want to check what is the
    > session value
    >
    > echo $_COOKIE["qrv"];
    >
    > it is null, but the actual cookie in the browser is set to yes or no.
    > When I try again the echo command give me the right output.
    >
    > Problem is that I always have to refresh the webpage in order to get
    > the cookie value.
    > It looks like php reads the cookie that was set before you load the
    > browser page.[/color]

    It's the old story:
    Javascript runs on the users computer, PHP on the server. PHP sends output
    "as is" to the client, with the variables available.
    Because the cookie is set AFTER the user has gotten the page, at the time
    the page was set this cookie variable wasn't available. No way to "fix"
    that, that's just how it works. If you need this functionality, it needs a
    different approach. Either create the dynamic content using javascript (for
    instance XMLHTTPRequest) , or let the user go to a different page.

    Grtz,
    --
    Rik Wasmus


    Comment

    • Henk Verhoeven

      #3
      Re: passing javascript cookies into php script

      Bart wrote:[color=blue]
      > (..)
      > Problem is that I always have to refresh the webpage in order to get
      > the cookie value.
      >(..)
      > Any ideas how to fix that?[/color]

      Sounds like you need AJAX!

      Comment

      • Bart

        #4
        Re: passing javascript cookies into php script

        Thanks I will try other options.

        Comment

        • Bart

          #5
          Re: passing javascript cookies into php script

          is there a easy way to refresh a page.

          Comment

          Working...