i have a form with multiple checkbox. how to get the values that are checked.

pls give a quick reaply.

thanks

Recommended Answers

All 9 Replies

var checkBox = document.getElementById(boxID);
checkBoxValue = checkBox.value;

Or if you are talking about a group of checkboxes with the same name you would do something like this:

page_html.php

<form method="post" action="">
<input type="checkbox" id="things[]" value="red"> Cat<br>
<input type="checkbox" id="things[]" value="blue"> Mouse<br>
<input type="checkbox" id="things[]" value="green"> Car<br>
<input type="checkbox" id="things[]" value="yellow"> DaniWeb
</form>

page_php.php

<?php
$things = serialize($_POST['things']);

$query = "INSERT INTO table(things) values($things)";
mysql_query($query) or die(mysql_error());
?>

basically just turn it into an array and dump the crap into a db. Notice that you have to "serialize" the data first and don't forget to "unserialize" when you are taking the data back out of the db (selecting).

You will then most likely have to implement a loop when you take the "crap" out of the db (as it will be in an array). The following link is a really good article on this, I would check it out.

Storing form array data to MySQL using PHP

Instead of id use name in ur HTML

<form method="post" action="">
<input type="checkbox" name="things[]" value="red"> Cat<br>
<input type="checkbox" name="things[]" value="blue"> Mouse<br>
<input type="checkbox" name="things[]" value="green"> Car<br>
<input type="checkbox" name="things[]" value="yellow"> DaniWeb
</form>

And post.php

<?php
$checkBox = $_POST['things'];
for($i=0; $i<sizeof($checkBox); $i++){
    $query = "INSERT INTO table(things) values('".$checkBox[$i]."')";
    mysql_query($query) or die(mysql_error());
}
?>
<form action='page.php' method='post'>
<input type='checkbox' name='chk' value='value1'> ANY VALUE 1 <br>
<input type='checkbox' name='chk' value='value2'> ANY VALUE 2 <br>
<input type='checkbox' name='chk' value='value3'> ANY VALUE 3 <br>
<input type='checkbox' name='chk' value='value4'> ANY VALUE 4 <br>
<input type='submit' name='submit' value='Get Value'>
</form>

page.php codes:

<?php
  
   $value = $_POST['chk'];

   echo $value;

?>
<input name='checkbox[]' type='checkbox' id='checkbox[]' value=\"".$row['id']."\"> 

Button at the bottom of form:

<input name='delete' type='submit' id='delete' value='Delete'> 
<?php

if(isset($_POST['delete'])){ 
for($i=0;$i<$count;$i++){ 
 $delete = $_REQUEST['delete']; 
 $checkbox = $_REQUEST['checkbox']; 
 $count = count($_REQUEST['checkbox']); 
$del_id = $checkbox[$i]; 
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; 
$result = mysql_query($sql); 
} 

// if done than auto refresh page 
if($result){ 
echo "<meta http-equiv=\"refresh\" content=\"0;URL=checkbox.php\">"; 
} 
mysql_close(); 
?>

is there possible to use session and cookie at a time

What if there is multiple checkbox and there name are different and due to some resone we can't make them in as array[]..how to fetch such checkbox from database n display????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.