By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Checkboxes

P: n/a
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?

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
'bonehead <se*********@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/
Jul 17 '05 #2

P: n/a
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
'bonehead <se*********@here.org> writes:

$checked = $foo ? "checked" : "";


Although, in the newer implementations of HTML, that would be

$checked = $foo ? 'checked="true"' : '';

Christopher Finke
Jul 17 '05 #3

P: n/a
"Christopher Finke" <ch**************@hotmail.usenet.com> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
'bonehead <se*********@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/
Jul 17 '05 #4

P: n/a
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".

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.