Checkboxes

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-26-2003
'bonehead
 
Posts: n/a
Default Checkboxes

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?

Reply With Quote
  #2 (permalink)  
Old 11-26-2003
Michael Fuhr
 
Posts: n/a
Default Re: Checkboxes

'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/
Reply With Quote
  #3 (permalink)  
Old 11-26-2003
Christopher Finke
 
Posts: n/a
Default Re: Checkboxes

"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


Reply With Quote
  #4 (permalink)  
Old 11-26-2003
Michael Fuhr
 
Posts: n/a
Default Re: Checkboxes

"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/
Reply With Quote
  #5 (permalink)  
Old 11-26-2003
bonehead
 
Posts: n/a
Default Re: Checkboxes

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".

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0