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
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    can't make checkboxes sticky

    Hi

    I have a form which contains 4 select boxes (amongst the usual input types)

    Here's an example

    Code:
    <div class="formElement"><p class="formText">Are you a tenant:</p><input class="checkbox" type="checkbox" name="resid[0]" value="is-tenant" /><div class="clearer"></div></div>
    <div class="formElement"><p class="formText">Are you a landlord:</p><input class="checkbox" type="checkbox" name="resid[1]" value="is-landlord" /><div class="clearer"></div></div>
    Now, I want to make these stick so that they are checked if the user presses submit, but forgets to fill in say their name etc

    Here's what I tried
    Code:
    <div class="formElement"><p class="formText">Are you on benefits:</p><input class="checkbox" type="checkbox" name="resid[0]" value="is-on-benefits" <?php if ($_POST["resid[0]"]=="is-on-benefits" ){ echo 'checked="yes" ';} ?>/><div class="clearer"></div></div>
    which didn't work...

    along with

    Code:
    <div class="formElement"><p class="formText">Are you on benefits:</p><input class="checkbox" type="checkbox" name="resid[0]" value="is-on-benefits" <?php if ((isset($_POST["resid[0]"]) ){ echo 'checked="yes" ';} ?>/><div class="clearer"></div></div>
    and putting !empty instead of isset.

    I guess I am doing something wrong?

    Can anyone help please

    Thanks

    Edward

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's not right. This is a multi-dimensional array, you would get to it from $_POST['resid'][0] for example. It only has value though if you got here from another location which posts the same field properties. If you did, you can use isset($_POST['resid'][0]) ? ' checked="checked"' : '' which should work as you need. Checkboxes will only be considered successful if checked, so the 0th offset is only available if the box was actually checked, so the isset is sufficent.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the reply. I;ve got it to work using:
    Code:
    <div class="formElement"><p class="formText">Are you a landlord:</p><input class="checkbox" type="checkbox" name="comm[0]" value="is-landlord"   <?php if (isset($_POST["comm"][0])){echo 'checked="checked" '; }?> /><div class="clearer"></div></div>
    However, using the ternary notation it doesn't work. I'd like to get to grips with this notation so plesae what is wrong?

    Code:
    <div class="formElement"><p class="formText">Are you a tenant:</p><input class="checkbox" type="checkbox" name="comm[1]" value="is-tenant"   <?php isset($_POST["comm"][1]) ? 'checked="checked" ':'' ?> /><div class="clearer"></div></div>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's because you have no output in the ternary operator. It simply has a string of 'checked="checked"' but it isn't printed or anything.
    PHP's ternary is unusual though; it runs ltr instead of rtl like it's supposed to, so nesting them is a disaster without lots of brackets.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    maxelcat (01-23-2013)


  •  

    Posting Permissions

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