This is a discussion on Checkboxes within the PHP Language forums, part of the PHP Programming Forums category; Okay, I've only been doing php/mysql for a few months and I admit to being a little stumped ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Okay, I've only been doing php/mysql for a few months and I admit to
being a little stumped on this one. I have a form which posts updates to a mysql table. I'd like to use checkboxes to show and update boolean type values. In Access it's very simple to create a field of type Yes/No, bind a form to the table, and bind a checkbox object to the Yes/No field. I am unable to find generic examples of this in php/mysql. 1. What field type should I use in a mysql table? 2. When the php<html><form> loads the data from the selected record, what is the generic syntax for initializing the check box? |
|
|||
|
'bonehead <senmenospam@here.org> writes:
> I have a form which posts updates to a mysql table. I'd like to use > checkboxes to show and update boolean type values. In Access it's very > simple to create a field of type Yes/No, bind a form to the table, and > bind a checkbox object to the Yes/No field. I am unable to find generic > examples of this in php/mysql. > > 1. What field type should I use in a mysql table? MySQL has a BOOL type but it's just a synonym for TINYINT(1). Another possibility would be ENUM. > 2. When the php<html><form> loads the data from the selected record, > what is the generic syntax for initializing the check box? Here's one way: // Set the variable $foo from the database or from form data. $checked = $foo ? "checked" : ""; echo "<input type=\"checkbox\" name=\"foo\" $checked>"; Or later if you're outside PHP: <input type="checkbox" name="foo" <?php echo $checked?>> Or you could omit the $checked variable: <input type="checkbox" name="foo" <?php echo $foo ? "checked" : ""?>> There are several ways to do this; the important thing is to print the "checked" attribute if the boolean variable is on/true. -- Michael Fuhr http://www.fuhr.org/~mfuhr/ |
|
|||
|
"Michael Fuhr" <mfuhr@fuhr.org> wrote in message
news:3fc43cf0$1_3@omega.dimensional.com... > 'bonehead <senmenospam@here.org> writes: > > $checked = $foo ? "checked" : ""; Although, in the newer implementations of HTML, that would be $checked = $foo ? 'checked="true"' : ''; Christopher Finke |
|
|||
|
"Christopher Finke" <christopherfinke@hotmail.usenet.com> writes:
> "Michael Fuhr" <mfuhr@fuhr.org> wrote in message > news:3fc43cf0$1_3@omega.dimensional.com... > > 'bonehead <senmenospam@here.org> writes: > > > > $checked = $foo ? "checked" : ""; > > Although, in the newer implementations of HTML, that would be > > $checked = $foo ? 'checked="true"' : ''; Shouldn't that be 'checked="checked"'? That's what the XHTML 1.0 DTD appears to require, and using "true" causes XHTML 1.0 and 1.1 documents to fail validation at validator.w3.org. -- Michael Fuhr http://www.fuhr.org/~mfuhr/ |
|
|||
|
Michael Fuhr wrote:
> Here's one way: > > // Set the variable $foo from the database or from form data. > $checked = $foo ? "checked" : ""; > echo "<input type=\"checkbox\" name=\"foo\" $checked>"; > > Or later if you're outside PHP: > > <input type="checkbox" name="foo" <?php echo $checked?>> Got it working...thanks. The exact syntax for the checked argument is where I was getting stumped. Meanwhile, I came up with another fairly trivial question. I'll post it under the heading "Textarea Formatting". |