How to Create a Cookie on <a href> tag using PHP?

Discussion in 'PHP' started by FR3@K, Oct 3, 2007.

  1. #1
    I am trying to set a cookies when someone clicks a link.
    and i will retrieve this cookie on body tag using something similar to this:

    <html>
    <body<?php
    if (isset($_COOKIE["wide1"]))
      echo " id=" . $_COOKIE["wide1"]"";>
    Code (markup):

    so can someone tell me how to set the cookies on a href tag?
    the tag is :

    <a href="javascript:void(0)" title="size"></a>
    Code (markup):
    i need to make it like this.

    <a href="javascript:void(0)" title="size" onclick="<?php 
    setcookie("wide1", "Medium", time()-3600);
    ?>"></a>
    Code (markup):
    any kind of help will be appreciated.
     
    FR3@K, Oct 3, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You can not mix onclick, or onxxxx attributes with PHP. The file will be parsed on the server and the output between the <?php will be sent to the client's browser. So your onclick attribute would be empty, and in your case you'd get a PHP error for headers already sent.

    Do you want this to happen live without refreshing the page?

    If so, you need javascript alone for this, and you should post it in the other section.

    Otherwise pass a GET variable to your page, and make PHP set the cookie if the variable has been passed.

    
    
    if ($_GET['size'] == 'medium')
    {
        setcookie("wide1", "Medium", time() + 3600);
    }
    
    PHP:
    Also, you were setting the cookie with a negative timestamp. So it would be directly erased on the browser.

    
    <a href="?size=medium" title="size"></a>
    
    HTML:
     
    nico_swd, Oct 3, 2007 IP
  3. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you dont understad what i wrote , then please dont reply.
    you gave a totally wrong reply.
    read my 1st post again and then reply.
     
    FR3@K, Oct 3, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    What the fuck is wrong with people today?

    Okay...

    Clear question... I understood. The answer is provided above. If you don't understand simple answers like mine above then don't post on forums. Hire someone to code everything for you.

    Sorry for taking the time trying to help you. It won't happen again, I promise.

    Obviously your question is not as clear as you think. I asked you a few things and it got ignored.


    People like you make me mad. Really mad... *negative reps*


    Pathetic kids...
     
    nico_swd, Oct 3, 2007 IP
  5. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry about my behaviour mate , i was frustrated.
    i was working on my site since morning and couldnt figure out how to use php to set cookies.
    but still you are wrong.
    you didnt tell me how will i retrieve these cookies.
    these cookies will be used to set the width of page.
    there are 3 options on my site.
    i will need to use php code 3 times.
    please tell me how will i retireve the cookies on my body tag?

    and read the post again .
    i said <a href="javascript:void(0)" title="size"></a>
    so the href's value should not be changed !
    it should just update the cookies when clicked and do its regular job(change the size of page)
     
    FR3@K, Oct 3, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Lol?

    You never asked how to receive them, in fact you posted it in your very first code, so I would have expected that you know how to do that?

    That was why I asked if you wanted to do it live, without refreshing the page. I told you what you need but you didn't like that reply and asked me to not reply again, lol.

    If you want, have a look at this page: http://www.php.net/cookies - Explains everything about cookies.

    I'm done with this here, and I've set you on the ignore list to make sure to not waste my time next time you need help.


    Bye.
     
    nico_swd, Oct 3, 2007 IP
  7. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    the link u gave me doest explain anything either.
     
    FR3@K, Oct 3, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Honestly, I don't know why I'm still replying...

    You don't deserve help if you're not willing to do any effort yourself. You don't even take the time to listen and answer the questions I ask.

    All of your questions have been answered. (Yes they have, I told you what to do and what you need)

    The link explains how to set and receive cookies with PHP (you're in the PHP section and asked how to do that with PHP). I told you that PHP cannot set cookies "onclick" without refreshing the page and I told you that you need javascript for that.

    What do you want from me?


    And lol at giving me negative rep, calling me mate and continue asking me to help. You're still on my ignore list, I just couldn't resist clicking "show post" to see what you said.

    I'm SERIOUSLY done now.
     
    nico_swd, Oct 3, 2007 IP
  9. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    but i am a newbie at php , i started working on it about a week ago.
    i only know 1 php statement.
    <?php include(); ?>

    and yes there must be a way to do it without a javascript line.
     
    FR3@K, Oct 3, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Okay... Maybe I'm not done yet, lol.

    No excuse for your behavior. Besides, if the only thing you know how to use is include(), then how do you have the balls to jump on me telling me everything I say is wrong?

    :rolleyes:


    How old are you, 13?
     
    nico_swd, Oct 3, 2007 IP
  11. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    um.. i am 15 :eek: hehehe..
    but i only said that u were wrong becuz u changed the a href code to href=?size=medium.
    and this wont work with my size changing script.
    it needs this :
    href="javascript:void(0)"

    and can i add you on my msn?
     
    FR3@K, Oct 3, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    Definitely not. You don't trust me anyway, so the point is...?

    I told you three times what to do now. Do it? I wouldn't tell you anything else on MSN either.
     
    nico_swd, Oct 3, 2007 IP
  13. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    but i didnt understand what u told me to do..
    please.. cant you help me?
     
    FR3@K, Oct 3, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    It's clear, and understandable English:

    Which part of that don't you understand?


    And lol at adding me on MSN anyway, after I told you I didn't want to. Request rejected...
     
    nico_swd, Oct 3, 2007 IP
    ansi likes this.
  15. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    er what am i supposed to do?
    i dont know javascript either.
    i didnt get you, may i pm you my size changing code?
    then you can tell me what to do with it.
     
    FR3@K, Oct 3, 2007 IP
  16. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #16
    nico_swd, Oct 3, 2007 IP
  17. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #17
    FR3@K, nico_swd is one of the most knowledgeable--and one of the most helpful--PHP programmers on DP. You are not going to get anyone to help you by pissing him off.

    He is trying to tell you that there is a difference between server-side code (PHP, which is executed prior to sending the page to the browser) and client-side code (JavaScript, which is executed in the user's browser after it receives the page).

    If you want to do it using PHP, you may have to send the user through a PHP function or script that sets the cookie. That means refreshing the page or sending the user through another page or script.

    If you want to do it using the onclick event, you should set the cookie using JavaScript.

    You are trying to mix the two, which is not the correct way to do it.
     
    TechEvangelist, Oct 3, 2007 IP
  18. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    ok wait i am searching on google for javascript cookies.
    are you sure this will do?? the javascript thing?

    and @ TechEvangelist , i already appoligized for my behavior.
    btw thanks for advices nico_swd , you really helped me , and i got the javascript to set cookies :D , thanks for help.
    +rep for you.
     
    FR3@K, Oct 3, 2007 IP
  19. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #19
    Thank you, TechEvangelist. :D I would rep you but I did spread too much today.

    :rolleyes: The world shall never know.

    Lie, because you gave me negative rep today and it won't let you give me more for a while. But it doesn't matter, I'm not rep addicted. All I care for is respect, which you failed to give me several times today.


    Though, glad you got it sorted and it's finally over. It was getting on my nerves.
     
    nico_swd, Oct 3, 2007 IP
  20. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    what ?? i didnt give you a negative rep :confused:
    and i didnt know how to five rep , i thought it was itrader.

    and that javascript cookie thing didnt work for me either :(
     
    FR3@K, Oct 4, 2007 IP