PHP - how to bind DB value to dynamic checkbox value

Discussion in 'PHP' started by thilagavathi, Jul 18, 2012.

  1. #1
    Hi,

    im a begineer of PHP.

    I tried create a library online shoping cart.

    i get $id value from DB...
    <input type="checkbox" name="Bookname[]" value="<?PHP echo $id ;?>"><?PHP echo $id ;?>..This is dynamic checkbox, which means i have three rows in table automatically display three checkbox one by one...

    what my doubt is.. i want to check this checkbox is checked means i want to do some action.. but i tried.. still im not getting any idea... please tel me which is good one..

    Thanks.
     
    thilagavathi, Jul 18, 2012 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    You need to check whether the database value for that field name is set to the value you have set it to as checked.

    Example:
    
    <?php
    function checkbox_checked($value,$compare) {
    
    	if($value == $compare) {
    		
    		echo ' checked';	
    		
    	}
    
    }
    ?>
    
    PHP:
    
    <input type="checkbox" name="field_name_here" value="value_here"<?php echo checkbox_checked('value_here',$db_value_for_field_here); ?> />
    
    HTML:
     
    HuggyEssex, Jul 18, 2012 IP