How to read multiple checkboxes ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavan52
    New Member
    • Apr 2013
    • 10

    How to read multiple checkboxes ?

    I have 1 form in with multiple checkboxes in it (each with the code):

    Code:
    <input type="checkbox" value="$cat_tit['category_title']" name="check_list[]" id="cat_title" />

    How would I be able to tell which checkboxes have been checked? (Maybe multiple)

    (In context:) Its for an inbox system and I have a button below that I want (when clicked) to save all values into the database which have the checkbox's checked...

    How would I do this?

    Thanks alot...
  • Ammu
    New Member
    • Aug 2011
    • 78

    #2
    Code:
    if(isset($_POST['submit']))
    {
    	for($i=0;$i<=count($_POST['check_list']);$i++)
    	{
            echo $_POST['check_list'][$i];
            }
    }

    Comment

    • pavan52
      New Member
      • Apr 2013
      • 10

      #3
      Hey thanks Ammu For ur rply m trying to print it like this
      Code:
      <?php
      
      if(!empty($_POST['check_list']))
      {
          foreach($_POST['check_list'] as $checkbox )
          {
          	echo $checkbox;        
          }
      }
      ?>
      But here if I select 5 checkboxes then its printing like this instead of printing the titles....

      $cat_tit['category_title ']$cat_tit['category_title ']$cat_tit['category_title ']$cat_tit['category_title ']$cat_tit['category_title ']


      Here is the full code ......

      Code:
      <?php 
      	$con=mysql_connect($host,$username,$password);
      	$qr2=mysql_select_db($db);?>
      	<form id="form" action="#" method="post"> 
      	
      	<?php $category_id = 1;
      	while($category_id <= 30)
      	{
      	$result = mysql_query("SELECT * FROM category_tbl where category_id = $category_id ");
      	$row = mysql_fetch_assoc($result);
      	echo '<div id="main">';
      	echo '<div class="content">';
      	echo '<div class="title">';
      	echo $row['category'];
      	echo '</div>';
      	echo '<div class="image"><button onclick="toggle5(showDiv'.$category_id.')" ><strong>Expand</strong></button>';
      	echo '</div>';
      	echo '<div id="showDiv'.$category_id.'" style="display:block">';
      	
      		$cat_title = mysql_query("SELECT * FROM category_title_tbl WHERE category_id = '$category_id'") or die (mysql_error()); 
                  while ($cat_tit=mysql_fetch_array($cat_title))
      			{?>    
      		         
                   <input type="checkbox" value="$cat_tit['category_title']" name="check_list[]" id="cat_title" />
                   <?php echo $cat_tit['category_title'];
      			 echo '<br />';
                  
      			}   
      	echo '</div>';
      	echo '</div>';
      	echo '</div>';
      	$category_id = $category_id + 1;
      	}?>
      	<input type="submit" value="Submit" name="submit" id="submit" />
      	</form>
      <?php
      
      if(!empty($_POST['check_list']))
      {
          foreach($_POST['check_list'] as $checkbox )
          {
          	echo $checkbox;        
          }
      }
      ?>

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        In your PHP code on line 24, it's not within PHP tags. But also, if you want to expand an array variable in a string, you need to remove the single quotes.

        Comment

        • Ammu
          New Member
          • Aug 2011
          • 78

          #5
          Code:
          <input type="checkbox" value="<?=$cat_tit['category_title']?>" name="check_list[]" id="cat_title" />

          Comment

          • pavan52
            New Member
            • Apr 2013
            • 10

            #6
            Hey Thanks Ammu Its Working Thanks Alot....

            Comment

            Working...