Checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 'bonehead

    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?

  • Michael Fuhr

    #2
    Re: Checkboxes

    'bonehead <senmenospam@he re.org> writes:
    [color=blue]
    > 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?[/color]

    MySQL has a BOOL type but it's just a synonym for TINYINT(1).
    Another possibility would be ENUM.
    [color=blue]
    > 2. When the php<html><form> loads the data from the selected record,
    > what is the generic syntax for initializing the check box?[/color]

    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

    Comment

    • Christopher Finke

      #3
      Re: Checkboxes

      "Michael Fuhr" <mfuhr@fuhr.org > wrote in message
      news:3fc43cf0$1 _3@omega.dimens ional.com...[color=blue]
      > 'bonehead <senmenospam@he re.org> writes:
      >
      > $checked = $foo ? "checked" : "";[/color]

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

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

      Christopher Finke


      Comment

      • Michael Fuhr

        #4
        Re: Checkboxes

        "Christophe r Finke" <christopherfin ke@hotmail.usen et.com> writes:
        [color=blue]
        > "Michael Fuhr" <mfuhr@fuhr.org > wrote in message
        > news:3fc43cf0$1 _3@omega.dimens ional.com...[color=green]
        > > 'bonehead <senmenospam@he re.org> writes:
        > >
        > > $checked = $foo ? "checked" : "";[/color]
        >
        > Although, in the newer implementations of HTML, that would be
        >
        > $checked = $foo ? 'checked="true" ' : '';[/color]

        Shouldn't that be 'checked="check ed"'? 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.or g.

        --
        Michael Fuhr

        Comment

        • bonehead

          #5
          Re: Checkboxes

          Michael Fuhr wrote:
          [color=blue]
          > 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?>>[/color]

          Got it working...thank s. 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".

          Comment

          Working...