Retaining checkbox state on redisplay

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jankie
    New Member
    • May 2007
    • 58

    Retaining checkbox state on redisplay

    Greetings everyone !
    I seek some help to retain checkbox state on redisplay.I only have one optional checkbox to indicate whether it's the user's first visit to the site

    Form1:
    [code=html]
    <INPUT type="checkbox" name="guest" value="yes">
    [/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    I tried it as below on Form2,but didnt work for me

    Form2:
    [code=html]
    <INPUT type="checkbox" name="guest" value="yes" <?php if(guest == "yes"){echo " CHECKED";}?>>[/code]

    Any ideas?

    Thank you
    Last edited by pbmods; Jun 18 '07, 09:01 PM. Reason: Added CODE tags.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    it would be:
    [PHP]
    if($_POST["guest"] == "yes")
    [/PHP]

    or:
    [PHP]
    if($_GET["guest"] == "yes")
    [/PHP]

    if your first form has the method set to post then use the first example, if it is set to get then use the second example.

    good luck

    Comment

    • Jankie
      New Member
      • May 2007
      • 58

      #3
      Thank you very much Epots9 ! really appreciate it
      And good luck to you, too

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Jankie.

        If you're curious, when you do this:
        Originally posted by Jankie
        [code=php]<?php if(guest == "yes"){echo " CHECKED";}?>[/code]
        What PHP is actually doing is looking for a constant named 'guest'. Since (presumably), you didn't define a constant (using the define keyword), PHP will instead use the string literal 'guest'. Since 'guest' will never == "yes", your script will never echo " CHECKED".

        Here's an explanation of why this happens (used to explain array indexes, but the principle is the same for strings):


        P.S., for full XHTML compliance, you should echo " checked=\"check ed\"" instead ~_^

        Comment

        • Jankie
          New Member
          • May 2007
          • 58

          #5
          pbmods
          Interesting explanation,
          i actually meant it the correct way,but sometimes as it turns out in my first code,the concept alone does not kill it,php-aware is essential
          Thank you all for sharing this insight !

          Comment

          Working...