maxelct Posted January 22, 2013 Share Posted January 22, 2013 Hi I have a form which contains 4 select boxes (amongst the usual input types) Here's an example <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 <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 <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 Link to comment https://forums.phpfreaks.com/topic/273493-cant-make-check-boxes-sticky/ Share on other sites More sharing options...
Barand Posted January 22, 2013 Share Posted January 22, 2013 try "checked='checked'" Link to comment https://forums.phpfreaks.com/topic/273493-cant-make-check-boxes-sticky/#findComment-1407510 Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2013 Share Posted January 22, 2013 The data will be in $_POST['resid'][0] Link to comment https://forums.phpfreaks.com/topic/273493-cant-make-check-boxes-sticky/#findComment-1407511 Share on other sites More sharing options...
Psycho Posted January 22, 2013 Share Posted January 22, 2013 Additionally, checkbox elements are not sent in the POST data if they are not checked. Since your checkboxes have unique names (i.e. you are explicitly defining the array index) you can do a simple isset() check to determine whether they are checked or not. In fact, using $_POST["resid[0]"]=="is-on-benefits" Would produce a PHP warning message if you had not checked that option. The warning should be suppressed in a production environment, but it's best - IMHO - to not have them in the code. Link to comment https://forums.phpfreaks.com/topic/273493-cant-make-check-boxes-sticky/#findComment-1407514 Share on other sites More sharing options...
maxelct Posted January 23, 2013 Author Share Posted January 23, 2013 Thanks guys - realized I had the syntax wrong for getting the array element. All works now! Link to comment https://forums.phpfreaks.com/topic/273493-cant-make-check-boxes-sticky/#findComment-1407665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.