Re: Ckeckbox list populated from database

From: Date: Sun, 20 Sep 2009 05:47:40 +0000
Subject: Re: Ckeckbox list populated from database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-298187@lists.php.net to get a copy of this message
On Sat, Sep 19, 2009 at 09:06:14PM -0700, Haig Davis wrote: > Hello List, > > I'm relatively new to PHP although I do feel I've got a grasp of the > basics. I have used php to draw up numerous selection lists populated > from a mysql database and have had no trouble at all. > > However when it comes to the same concept yet using HTML check boxes I > aM complety stumped. It seems easy but I've been staring at the same > problem for hours. > > Any hints are greatly appreciated. I don't know what type of field you're trying to use to populate the checkbox, but it basically comes down to this: <input type="checkbox" name="pizza" <?php if ($fieldval == 'something') echo 'checked="checked"'; ?>/> or <input type="checkbox" name="pizza" <?php echo ($fieldval == 'something) ? 'check="checked"' : ''; ?>/> You've got to decide what is "checked" and "unchecked" for your checkbox, and then make that a condition which triggers a "checked" or "unchecked" state for the box. If you like, you can also put a value="something" attribute in the input tag as well. When your user is finished inputting the form and you go to process the results, you can check for the existence of the checkbox name in the $_POST variable, or check that the $_POST variable contains the value you set in the value="something" attribute. Checkboxes and radio buttons are tricky in this way, because they don't normally return values in the same way an <input type="text" ...> field does. Paul -- Paul M. Foster

« previous php.general (#298187) next »