SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checked dynamic check box

    sorry i'm new in php
    i have created dynamic checkbox
    PHP Code:
    $rsc mysql_query($sqlc);

    while(
    $rowc mysql_fetch_array($rsc))
    {
        
    echo 
    "<input type=\"checkbox\" name=\"dataid\" value=\"".$rowc["data_no"]."\" />"; echo $rowc['data_name'];
            echo 
    "<br />";

    Whether it have value or not be not checked how solved this?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unchecked checkbox:

    Code HTML4Strict:
    <input type="checkbox" name="dataId" value="1" />

    Checked checkbox:

    Code HTML4Strict:
    <input type="checkbox" name="dataId" value="1" checked="checked" />

    That should give you a hint where you went wrong

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what ???

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by altarek View Post
    what ???
    Is that all you have to add?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    What furicane gave you is the completed, full code for a checked checkbox.
    So you need to check if there is a value in the database and then make your code come out like his.

    eg
    PHP Code:
    if($rows['data_no'] != '') {
        
    $isChecked ' checked="checked"';
    } else {
        
    $isChecked '';
    }
    echo 
    "<input type=\"checkbox\" name=\"dataid\" value=\"".$rowc["data_no"]."\" $isChecked />"; echo $rowc['data_name'];

            echo 
    "<br />"
    </span>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    foreach(range(110) as $num){
      
      
    #determine if checked here, we're doing it randomly
      
    $isChecked rand(01);
      
      
    printf(
        
    '<label for="%1$s">Box %2$d</label><input type="checkbox" id="%1$s" name="%1$s" %3$s />' "\n",
        
    'box-' $num,
        
    $num,
        
    $isChecked 'checked="checked"' ''
      
    );
      
    }
    Code:
    <label for="box-1">Box 1</label><input type="checkbox" id="box-1" name="box-1"  />
    <label for="box-2">Box 2</label><input type="checkbox" id="box-2" name="box-2"  />
    <label for="box-3">Box 3</label><input type="checkbox" id="box-3" name="box-3"  />
    <label for="box-4">Box 4</label><input type="checkbox" id="box-4" name="box-4"  />
    <label for="box-5">Box 5</label><input type="checkbox" id="box-5" name="box-5" checked="checked" />
    <label for="box-6">Box 6</label><input type="checkbox" id="box-6" name="box-6" checked="checked" />
    <label for="box-7">Box 7</label><input type="checkbox" id="box-7" name="box-7" checked="checked" />
    <label for="box-8">Box 8</label><input type="checkbox" id="box-8" name="box-8"  />
    <label for="box-9">Box 9</label><input type="checkbox" id="box-9" name="box-9"  />
    <label for="box-10">Box 10</label><input type="checkbox" id="box-10" name="box-10"  />
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd just suggest getting up to speed with HTML markup if you are not familiar with what some elements are used for and how to change their state (checked, unchecked, selected etc).

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Excellent advice furicane.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •