1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

set default check in a 'checkbox' form

Discussion in 'Programming' started by byoung, Aug 23, 2006.

  1. #1
    Working with a form that generates checkbox options from a database. I want to set a default option as checked (ticked).

    Does anyone know if i can do it or how to do it.
     
    byoung, Aug 23, 2006 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    when you are writing the checkbox - create a variable before it that is equal to "" then do an if statement to see if it should be checked. If it is - then set your variable = " checked "

    then when you write your (x)html just enter that variable which is either equal to "" or equal to " checked "
     
    ccoonen, Aug 23, 2006 IP
  3. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #3
    
    // DB pull
    $query = mysql_query("select value FROM ...");
    $checked;
    if ($row = mysql_fetch_assoc($query))
    {
        if (isset($row['value']))
        {
            $checked = "checked";
        }
    }
    <input type="checkbox" name="value" id="value" {$checked}>
    
    PHP:
     
    drewbe121212, Aug 25, 2006 IP
  4. phulla

    phulla Peon

    Messages:
    1
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use the following, obviously change to suit:

    
    <input type="checkbox" name="chkName" id="chkName" <?= ( isset($row[0]) ? ' checked="checked"' : '') ?> />
    
    PHP:
    or if you need the form to remember the values of a checkbox (or any other field)

    
    <input type="checkbox" name="chkName" id="chkName" <?= ( isset($_REQUEST['chkName']) ? ' checked="checked"' : '') ?> />
    
    PHP:
     
    phulla, Feb 13, 2008 IP
    toniya likes this.