Jump to content

Checkbox form field making sticky and remembering input


hbradshaw

Recommended Posts

Hello:

 

I'm trying to get a form with checkboxes to be sticky and to remember what the person checked.

 

I am using sessions and when I echo the results of the checkboxes the correct result appears. 

 

Thus far, all the code I've found makes all the boxes checked.  After the person clicks submit, I only want the boxes checked that the person checked.

 

Can someone help me out?  Thank you in advance.

 

This is the code that I have.

<input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br>  

<input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br>  

Hi, in the code you entered:

 

<input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br>  

<input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br>  

 

you have used the check value 'Weekday Mornings' for both the mornings and afternoons checkboxes, try this instead:

 

<input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br>  

<input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Afternoons' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br>  

 

Is that it, or was that just a typo on the post? Also, I would recommend using radio buttons rather than check boxes if you only want one checked at a time. Or, if you want to be able to have them both checked, you might want to use more than one session variable for them, but I don't know the context so I could be wrong.

OK, there are several problems here:

 

1. You are using availability[] as the name for both fields. I'm not sure how you are setting you session values, but you can't reference two independant values using the same variable!, i.e. $_SESSION['availability'] can't be two different things. I would think you would make $_SESSION['availability'] an array since it can have multiple values. So you should be using in_array().

 

2. You are using the same id for both fields. This probably isn't causing the problem you are having, but it is incorrect.

 

3. Your PHP is malformed. It should be like this:

 

echo ( isset($_SESSION['availability']) && in_array('Weekday Mornings' , $_SESSION['availability']) )? ' checked="checked"' : '';

 

 

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.