944,162 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 126087
  • PHP RSS
Nov 27th, 2006
1

get checkbox value

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

pls give a quick reaply.

thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fariba123 is offline Offline
2 posts
since Nov 2006
Nov 27th, 2006
0

Re: get checkbox value

var checkBox = document.getElementById(boxID);
checkBoxValue = checkBox.value;
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Nov 27th, 2006
-1

Re: get checkbox value

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
Reputation Points: 11
Solved Threads: 1
Newbie Poster
the_count is offline Offline
10 posts
since Nov 2006
Nov 27th, 2006
0

Re: get 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
PHP Syntax (Toggle Plain Text)
  1. <form method="post" action="">
  2. <input type="checkbox" id="things[]" value="red"> Cat<br>
  3. <input type="checkbox" id="things[]" value="blue"> Mouse<br>
  4. <input type="checkbox" id="things[]" value="green"> Car<br>
  5. <input type="checkbox" id="things[]" value="yellow"> DaniWeb
  6. </form>

page_php.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. $things = serialize($_POST['things']);
  3.  
  4. $query = "INSERT INTO table(things) values($things)";
  5. mysql_query($query) or die(mysql_error());
  6. ?>

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nickclarson is offline Offline
8 posts
since Oct 2006
Dec 15th, 2010
0
Re: get checkbox value
Instead of id use name in ur HTML
PHP Syntax (Toggle Plain Text)
  1. <form method="post" action="">
  2. <input type="checkbox" name="things[]" value="red"> Cat<br>
  3. <input type="checkbox" name="things[]" value="blue"> Mouse<br>
  4. <input type="checkbox" name="things[]" value="green"> Car<br>
  5. <input type="checkbox" name="things[]" value="yellow"> DaniWeb
  6. </form>

And post.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $checkBox = $_POST['things'];
  3. for($i=0; $i<sizeof($checkBox); $i++){
  4. $query = "INSERT INTO table(things) values('".$checkBox[$i]."')";
  5. mysql_query($query) or die(mysql_error());
  6. }
  7. ?>
Last edited by Ezzaral; Dec 15th, 2010 at 1:28 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muhammadsajid is offline Offline
2 posts
since Feb 2010
Dec 15th, 2010
0
Re: get checkbox value
PHP Syntax (Toggle Plain Text)
  1. <form action='page.php' method='post'>
  2. <input type='checkbox' name='chk' value='value1'> ANY VALUE 1 <br>
  3. <input type='checkbox' name='chk' value='value2'> ANY VALUE 2 <br>
  4. <input type='checkbox' name='chk' value='value3'> ANY VALUE 3 <br>
  5. <input type='checkbox' name='chk' value='value4'> ANY VALUE 4 <br>
  6. <input type='submit' name='submit' value='Get Value'>
  7. </form>


page.php codes:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $value = $_POST['chk'];
  4.  
  5. echo $value;
  6.  
  7. ?>
Last edited by Ezzaral; Dec 15th, 2010 at 1:29 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
lyrico is offline Offline
89 posts
since Dec 2010
Dec 15th, 2010
0
Re: get checkbox value
Reputation Points: 17
Solved Threads: 19
Junior Poster
cossay is offline Offline
121 posts
since Nov 2010
Feb 14th, 2011
0
Re: get checkbox 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();
?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kuldeep_singh is offline Offline
1 posts
since Feb 2011
Feb 14th, 2011
0
Re: get checkbox value
is there possible to use session and cookie at a time
Reputation Points: 10
Solved Threads: 0
Newbie Poster
navmalik is offline Offline
1 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Deleting item when your admin
Next Thread in PHP Forum Timeline: Neural Network training...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC