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 ?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.