How to tell if a checkbox is checked

Multiple delete statement from checkbox status.

Page 1 of 1

6 Replies - 14251 Views - Last Post: 21 April 2010 - 12:26 AM Rate Topic: -----

#1 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

How to tell if a checkbox is checked

Posted 19 April 2010 - 02:59 PM

Hey All,

I think its just late and my shrivelling brain just can't handle anymore code, but, i have a problem.

I have a page that is bringing back all the rows in a given database, i have a checkbox for each row brought back that has the value of the auto incremented id (cid in my case). Now i have a delete button, and the idea is check which rows i want to delete from the database then when i press delete, hey presto there gone.

My question is, when i click the submit button, how can i tell which checkboxes have been ticked, so i can pass these 'cid's' to the next page, or just process them on the same page in a statement.

Please see the part of my code that deals with this (form code)
include'../connect.php';
	$tbl_name="comments";

	mysql_connect("$host", "$username", "$password")or die("cannot connect to the DB");
	mysql_select_db("$db_name")or die("cannot select DB");
		
	$sql = "select * from $tbl_name order by cid asc";
	$results = mysql_query($sql);
		echo"<form id='ccform' method='post' action=''>";
	while($row = mysql_fetch_array($results)){
		echo"<div id='commentcheck'>";
		echo"<font color='white'>Name:</font>&nbsp;&nbsp;".$row['name']."&nbsp;<br/>";
		echo"<font color='white'>IP Addr:</font>&nbsp;&nbsp;".$row['ip']."&nbsp;<br/>";
		echo"<font color='white'>Timestamp:</font>&nbsp;&nbsp;".$row['timestamp']."<br/>";
		echo"<font color='white'>Comment:</font>&nbsp;&nbsp;".$row['comment']."&nbsp;<br/>";
		echo"<input type='checkbox' name='posts' value='".$row['cid']."'>&nbsp;&nbsp;Delete";
		echo"</div><br/><br/>";
	}
		echo"<input name='del' type='submit' value='Delete checked posts' height='10px' />";
		echo"</form>";



Please let me know if you require further explanation, or just have a question.

Many Thanks in advance.

Chopster

Is This A Good Question/Topic? 0
  • +

Replies To: How to tell if a checkbox is checked

#2 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: How to tell if a checkbox is checked

Posted 19 April 2010 - 03:52 PM

Form data can be retrieved in PHP using the array $_POST.

With that in mind, a checkbox will only set an array key if it is checked. Otherwise it will be undefined.

if (isset($_POST['checkbox'])
{
    // checkbox is checked
}
if (!isset($_POST['checkbox'])
{
    // checkbox is not checked
}

This post has been edited by Xuhybrid: 19 April 2010 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Scotty G  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 59
  • Joined: 09-April 10

Re: How to tell if a checkbox is checked

Posted 19 April 2010 - 04:36 PM

I've done something exactly like this. As Xuhybrid explained you want to post your checkboxes to an array. So you would post to a variable like seen below.

$myArray = $_POST['posts'];




Then make sure your checkboxs all have the same name with brakets [].


echo"<input type='checkbox' name='posts[]' value='".$row['cid']."'>&nbsp;&nbsp;Delete";



now I think this should work for you! try to
echo $myArray[0];

to see if the 'cid' is in there.

Hope this helps a bit!
Was This Post Helpful? 1
  • +
  • -

#4 LiDoNg_9_0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Re: How to tell if a checkbox is checked

Posted 19 April 2010 - 05:48 PM

in php, i think it automatically retrieve the checked value of option or check box..
but be sure that in your form, the name of your checkbox is in array type.
<form method="POST">
<input type="checkbox" name="post[]" value="blablabla"> 
</form>

//retrieving the value
<?php
$checkbox = $_POST['post']; ////depends on the method you use
?>


tat's it..
Was This Post Helpful? 0
  • +
  • -

#5 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: How to tell if a checkbox is checked

Posted 20 April 2010 - 11:49 AM

Apologies for the late response, i had to work all day, whats up with that?!!

But i am going to try these out now, they all sound like decent replys so thanks alot, will let you know when its complete.

Many Thanks

Chopster

This post has been edited by Chopster: 20 April 2010 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: How to tell if a checkbox is checked

Posted 20 April 2010 - 12:18 PM

Yeah got it all working now, thanks alot again guys.

Thought i would just post the code if someone else has problems with this and stumbles over it.

$sql = "select * from $tbl_name order by cid asc";
	$results = mysql_query($sql);
	$deleteArray = $_POST['posts'];
		echo"<form id='ccform' method='post' action=''>";
	while($row = mysql_fetch_array($results)){
		echo"<div id='commentcheck'>";
		echo"<font color='white'>Name:</font>&nbsp;&nbsp;".$row['name']."&nbsp;<br/>";
		echo"<font color='white'>IP Addr:</font>&nbsp;&nbsp;".$row['ip']."&nbsp;<br/>";
		echo"<font color='white'>Timestamp:</font>&nbsp;&nbsp;".$row['timestamp']."<br/>";
		echo"<font color='white'>Comment:</font>&nbsp;&nbsp;".$row['comment']."&nbsp;<br/>";
		echo"<input type='checkbox' name='posts[]' value='".$row['cid']."'>&nbsp;&nbsp;Delete";
		echo"</div><br/><br/>";
	}
		echo"<input name='del' type='submit' value='Delete checked posts' height='10px' />";
		echo"</form>";
	}
		print_r($deleteArray);


Many Thanks

Chopster
Was This Post Helpful? 0
  • +
  • -

#7 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: How to tell if a checkbox is checked

Posted 21 April 2010 - 12:26 AM

Glad i could help. Some browsers handle checkbox values differently so i use the isset() option.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1