Results 1 to 6 of 6

Thread: Getting value of checkbox in form build from database

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Getting value of checkbox in form build from database

    I got this page I want to use to edit records in the database. All text fields, and one bool (tinyint(1)) field to indicate whether the record is active or not.

    I build up a table of records from the database like this:
    Code:
    <?php          	
    	$count = $results->num_rows;	
    	while($row = $results->fetch_object())  {  
    ?>
    	<tr>
    		<td>
        	<input type="hidden" name="_count" value="<?php echo $count ?>"/>  
    			<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
    		</td>
    		<td>
    			<input name="lang_code[]" type="text" id="lang_code" value="<?php echo $row->lang_code; ?>" />
    		</td>
    		<td>
    			<input name="name_en[]" type="text" id="name_en" value="<?php echo $row->name_en; ?>" />
    		</td>
    		<td>
    			<input name="name_native[]" type="text" id="name_native" value="<?php echo $row->name_native; ?>" />
    		</td>
    		<td>
    			<input type="checkbox" <?php if($row->active) echo "checked='checked'"; ?> value="<?php echo $row->active; ?>" name="active[]" id="active" />
    		</td>      
    	</tr>      
    <?php }	?>
    and at the top of the page run the following if the page was posted:
    Code:
    for($i=0;$i< $_POST['_count'];$i++){
      $id=$_POST[id][$i];
      $lang_code=$_POST[lang_code][$i];
      $name_en=$_POST[name_en][$i];
      $name_native=$_POST[name_native][$i];
      $active=$_POST[active][$i];
      update_language($id,$lang_code,$name_en,$name_native,$active);
    }
    Everything works fine, it's just I cannot get whether the check box ($active) was checked or not.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Getting value of checkbox in form build from database

    If the checkbox is checked then its `value` attribute is sent to the server.

    PHP Code:
    <input type="checkbox" name="test" value="Bananas" />
    ...
    <?php
      
    if (isset($_POST['test']) && $_POST['test'] == 'Bananas')
        
    # ...
    ?>
    If it is not checked then its value is blank.

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Getting value of checkbox in form build from database

    guess i'm doing sth wrong.
    tried isset($_POST[active][$i]) without luck...will give it a break and check later. still asleep here
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Getting value of checkbox in form build from database

    do
    PHP Code:
    print_r($_POST
    and see what is being sent.

  5. #5

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Getting value of checkbox in form build from database

    I did the print_r($_POST) thing, and realized I don't see "active" in there.

    Then did print_r($_POST[language_code]); and get the values as it should be send.

    print_r($_POST[active]); returned nothing. So the array for the $active[] checkbox are not send.

    So for text fields the following work fine, but not for checkbox. Can anyone think what is wrong with the following:
    Code:
    while($row = $results->fetch_object())  {  ?>
    	<tr>
    		<td>              
    			<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
    		</td>
    		<td>
    			<input name="language_code[]" type="text" id="language_code" value="<?php echo $row->language_code; ?>" />
    		</td>
    		<td>
    			<input name="language_english[]" type="text" id="language_english" value="<?php echo $row->language_english; ?>" />
    		</td>
    		<td>
    			<input name="language_native[]" type="text" id="language_native" value="<?php echo $row->language_native; ?>" />
    		</td>
    		<td>
    			<input type="checkbox" <?php if($row->active) echo("checked='checked'"); ?> name="active[]" id="active" value="1" />
    		</td>      
    	</tr>      
    <?php }	?>
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Getting value of checkbox in form build from database

    If it is not checked, i think it doesn't get sent at all (not just a blank value). isset should return false.


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

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



Click Here to Expand Forum to Full Width