How can I get this check box to work?

the id='item' will always be the same per item no matter which I check. It always grabs the last one checked.


$result10 = mysql_query("SELECT * FROM todo WHERE complete='0' AND day='$today' ORDER BY whenby ASC LIMIT 3");
<form method="post" action="login-home.php" id="submittedtodo" >
		echo "This is the list for $todayshow </br>";
		while($row6 = mysql_fetch_assoc($result10))
		// item and when it is do by
	 	echo "<input type='checkbox' name='item' id='item' value=\"{$row6['id']}\">
	  	<label for=\"item\"><strong>Item:</strong> &nbsp;" . $row6['item'] . "&nbsp; (" . $row6['time'] . ") &nbsp; &nbsp;</label>";
			while($row7 = mysql_fetch_assoc($result11))
				// Employee Clocked In to do the Todo list
				echo "
				<input type=\"submit\" name=\"employeedo\" id=\"employeedo\" value=\"{$row7['employee']}\">";

then submitted

//Todo list
   $person = $_POST['employeedo'];
   $item = $_POST['item'];
mysql_query("UPDATE todo SET complete = '1', whodone = '$person', whendone = NOW() WHERE id = '$item'"); 

Recommended Answers

All 3 Replies

You should set atribute name of the checkbox to


so that you create a checkbox group. Upon submitting the form you will get all checked values in an array stored in $_SESSION. The array can be empty if none of the checkboxes were checked, so use empty() to check for values first. You loop through array and handle the values:

if(!empty($_SESSION['item'])) {

    echo "You clicked: ";
    foreach($_SESSION['item'] as $oneCheckboxValue) {

        // do something with the checkbox value, like echo
        echo oneCheckboxValue . ' ';

} else {

    echo 'None of the checkboxes has been clicked.';


Another thing not related to this question is that id atributes should be unique for each element. So make sure that id atributes change for each checkbox, maybe like in the following example:

$i = 1;

// your loop here

    echo "<input type='checkbox' name='item[]' id='item$i++' value=\"{$row6['id']}\" />;

// end of loop

That is how you wont have any troubles when using javascript in future. And also input elements do not have closing tag so they should be terminated by / (see above).

what does label for='item' need to be? if it changes for every "while"

and how do I get the "$oneCheckboxValue"

Member Avatar

The label links to the 'id' not the name and the id MUST be unique in each case. The name, as mentioned, should be set as an array if it makes sense to do so:

echo "<input type='checkbox' name='item[]' id='item{$row6['id']}' value=\"{$row6['id']}\">
	  	<label for=\"item{$row6['id']}\"><strong>Item:</strong> &nbsp;" . $row6['item'] . "&nbsp; (" . $row6['time'] . ") &nbsp; &nbsp;</label>";

I'm assuming that the row6id is an integer or unique single word. It may not work otherwise.

Now you just loop:

$items = (array) $_POST['item'];
foreach($items as $item){
  echo $item . "<br />";

You need to clean your post data though - you can use intval() for integers?

Be a part of the DaniWeb community

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