Jump to content

can't make check boxes sticky


maxelct

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.