Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Setting Required fields

    Hi All,

    Say I have a form with required fields on a website. If one of the fields is not filled in upon submit they values entered in other textboxes keep what is written in them:

    PHP Code:
    <input type="text" name="email" id="email" value="<?php echo $email?>" style="width:200px;" />
    But how do you do this with a tickbox?

    In my form my tickboxes have a value of yes is indicated, so if they are ticked but a required form field is not filled in how do I keep that ticked value, or can I keep it?

    PHP Code:
    <input type="checkbox" name="the_value" value="Yes"
    Thanks

    Chris

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You just need to add the HTML syntax that defaults a checkbox to checked, which is checked="checked". Throw in an "if" statement to check if the checkbox post/get variable = "Yes" and if it is, add that string to the checkbox tag.

    The trick to remember about checkboxes is the get/post variable will not exist at all if the checkbox is not checked. If it's checked, it's there.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Thanks kinda get it, something like?:

    PHP Code:
    <input type="checkbox" name="basic_exposure" checked="<? if ($the_value == '') echo ' Yes' ?>" value="Yes">
    But the form loads as the box ticked automatically?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    More like:

    PHP Code:
    <input type="checkbox" name="basic_exposure" <?php if ($basic_exposure == 'Yes') echo "checked='checked'"?> value="Yes">


  •  

    Posting Permissions

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