1

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

pls give a quick reaply.

thanks

10
Contributors
9
Replies
52
Views
10 Years
Discussion Span
Last Post by bhagyashree.bhatt2
0

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

1

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());
}
?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0
<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;

?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0
<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(); 
?>

Edited by Dani: Formatting fixed

0

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????

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.