Results 1 to 9 of 9

Thread: PHP is not reading cookie until I refresh - help

  1. #1
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP is not reading cookie until I refresh - help

    I am having the following problem. On a php script I am setting a cookie with javascript and a few lines lower I am trying to recall the value in php. The php is not getting the value unless I refresh the page. Can you please help me find a solution?
    My code is as follows...

    Code:
    <SCRIPT LANGUAGE=JavaScript>
    hash = parent.location.hash;
    ar=hash.substring(1);
    document.cookie = "video="+ar;
    </SCRIPT>
    
    <?php
    $ar = $_COOKIE["video"];
    echo "ar-->".$ar;
    ?>

    the reason I am doing this is because I dont know some other way to pass a variable from javascript to php (on the same script).
    I want to read a variable from the url not using GET or POST but using the hash (#) value.
    I am doing this because I want to change the URL from a click on a flash object without refreshing the page.
    Last edited by sic00; 05-20-2009 at 10:07 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I think it should be:
    Code:
    document.cookie = "video = "+escape(ar);
    That should work.

    (This works for me):
    Code:
    <script type="text/javascript">
    document.cookie = "greeting =" +escape("Hello");
    </script>
    <?php echo $_COOKIE['greeting']; ?>
    Jeremy | jfein.net

  3. #3
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you for your reply but unfortunatelly its not working.
    I even made a test file with your script..
    Code:
    <script type="text/javascript">
    document.cookie = "greeting =" +escape("Hello");
    </script>
    <?php echo $_COOKIE['greeting']; ?>
    but its not working either.
    It only works after I refresh.

  4. #4
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I 've read the following in the PHP manual:
    Cookies will not become visible until the next loading of a page that the cookie should be visible for.
    so.. I am afraid that its not possible to do what I want to do.
    is there any other way to pass the variable value from javascript to php?

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can do something like this:
    Code:
    <script type="text/javascript">
    var a = "food";
    </script>
    <?php
    $a = "<script type='text/javascript'>document.write(a);</script>";
    
    echo $a;
    ?>
    Or read about Ajax (search on google).
    Jeremy | jfein.net

  6. #6
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you very much Nile.
    thats what i was looking for...
    $a = "<script type='text/javascript'>document.write(a);</script>";

    it was very simple but i am not an experienced coder and i didnt know it was possible.. thank you..
    although i think i should learn some things about Ajax...

    I am making the following site www.ecogreens.tv
    and the reason i want to do this is that i want the left side of the page (the video player) to refresh when I click on a link on the right side (the swf with the categories and video list) but i want the swf to stay as it is.

  7. #7
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nile... another problem came up...
    i am using your method and the variable is being echoed fine.
    but when i am using it on a sql query a few lines lower i am getting an error..
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL...
    Code:
    $ar = "<script type='text/javascript'>document.write(ar);</script>";
    //$ar = '124356';
    echo "ar-->".$ar;
    $result = mysql_query ("SELECT thumb,title,descr FROM videos WHERE url='$ar' LIMIT 1");
    while($row = mysql_fetch_array($result))
    the echo works fine but i get the error on the while line.
    if i use the $ar = '124356'; things are working so there is no some other error...
    could you explain this?

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This is because it queries like this:
    Code:
    SELECT ... WHERE url='<script type='text/javascript'>document.write(ar);</script>'
    You need to look up ajax online for this to work.
    Jeremy | jfein.net

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You are approaching this backwards: PHP loads ENTIRELY and only after that will the javascript process. PHP is a generation program for text (in this case html/javascript), that is only after being generated passed to the browser. Then the browser can do what it wants. There is no possible way to run Javascript on a page before that page is parsed with PHP. Using Ajax will get you a way to send a variable back to the PHP AFTER it is parsed and save you having to refresh the page, but not before you load the page the first time.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •