954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

get checkbox value

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

pls give a quick reaply.

thanks

fariba123
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

if you are posting it using a PHP form
then you will use something like that


($_POST['checkboxname] == "ON")

if you want to see what checkboxes are checked you have to declare them as an array then loop through them

see this link

http://www.daniweb.com/techtalkforums/thread62312.html

the_count
Newbie Poster
10 posts since Nov 2006
Reputation Points: 11
Solved Threads: 1
 

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
<input type="checkbox" id="things[]" value="blue"> Mouse
<input type="checkbox" id="things[]" value="green"> Car
<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

nickclarson
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

Instead of id use name in ur HTML

<form method="post" action="">
<input type="checkbox" name="things[]" value="red"> Cat
<input type="checkbox" name="things[]" value="blue"> Mouse
<input type="checkbox" name="things[]" value="green"> Car
<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());
}
?>
muhammadsajid
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
<form action='page.php' method='post'>
<input type='checkbox' name='chk' value='value1'> ANY VALUE 1 
<input type='checkbox' name='chk' value='value2'> ANY VALUE 2 
<input type='checkbox' name='chk' value='value3'> ANY VALUE 3 
<input type='checkbox' name='chk' value='value4'> ANY VALUE 4 
<input type='submit' name='submit' value='Get Value'>
</form>

page.php codes:

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

   echo $value;

?>
lyrico
Junior Poster in Training
94 posts since Dec 2010
Reputation Points: 33
Solved Threads: 15
 
cossay
Junior Poster
128 posts since Nov 2010
Reputation Points: 17
Solved Threads: 22
 

Button at the bottom of form:

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

Kuldeep_singh
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

is there possible to use session and cookie at a time

navmalik
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You