Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.