Jump to content

Retaining checked status of checkboxes in a form


FeralReason

Recommended Posts

(HTML Form, using PHP) I have a form that, in the event of a user error, I want displayed again with the values just entered. I have solved this for the text boxes but, no matter what I try I have not been able retain the checked status of the checkboxes. Does anyone know how to do this ?

This should belong in the PHP forum, but here's my idea.

 

You create an associative array of values for those checkboxes.

 

Say you have this:

<input type="checkbox" name="foods[]" value="rice" /> Rice
<input type="checkbox" name="foods[]" value="ramen" /> Ramen
<input type="checkbox" name="foods[]" value="chicken" /> Chicken
<input type="checkbox" name="foods[]" value="pizza" /> Pizza

 

Then have this array (assuming you submitted the form in post method)

$food_values = array();
$foods = $_POST['foods'];
foreach ($foods as $value) {
     $food_values[] = $value;
}

 

That would set $food_values to have all the values of the checkboxes that are checked. I guess you can have another array that contains a list of all checkboxes. If you have them in the DB, query the DB, but you get the point right?

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.