Page 1 of 1

Checkboxes

Posted: Tue Oct 03, 2006 10:41 am
by aquanutz
Ok, I am generating checkboxes from entries I have in a database.. I can do this just fine. What I am stuck on is how do it tell if a user has checked that specific box in my other php files? I can't use $_Post[] can I? The reason that I assume that is because the id is always going to be different. It will always be the index of a date range from my database.

I orginally had a drop down populated with all the date ranges and that was fine since I could just see what the value of that box was in my other scripts... but being able to select more than one range would be a plus...

Here is the code I use to generate the checkboxes:

Code: Select all

$db = "calendar";

mysql_select_db($db) or die("Couldn't open DB: " .mysql_error());
					
$query = "select num, DATE_FORMAT(startDate,'%M %e, %Y') as startDate, DATE_FORMAT(endDate, '%M %e, %Y') as endDate from extendedDates";
					
$result = mysql_query($query) or die("Invalid query: " . mysql_error());
					
$p = "<ul>";

while($d = mysql_fetch_array($result))
{
	$id = $d['num'];
	$sdate = $d['startDate'];
	$edate = $d['endDate'];
						
	$p .= "<li><input name = ". $id . " type = \"checkbox\" id=" . $id . "/> $sdate - $edate </label>";
}
$p .= "</ul>";
					
echo $p;
Thanks for any thoughts

Posted: Tue Oct 03, 2006 10:46 am
by Burrito
checkboxes are either on or off (set or not set).

use the isset() function to determine if a box is checked or not.

and yes, use the $_POST[] array.

Posted: Tue Oct 03, 2006 10:52 am
by aquanutz
But my problem is that I will not know the id of each checkbox. For instance, one could be named "4" where another is "9". So in order to see if they are checked, I would have to check all 0 - 9 and that is something I was hoping to avoid. :oops:

Posted: Tue Oct 03, 2006 11:01 am
by Burrito
you could either do it in a loop, or the way I'd do it would be to create an array for the checkboxes and just give them different values.

ex:

Code: Select all

<input type="checkbox" name="myCheck[]" value="1">
<input type="checkbox" name="myCheck[]" value="2">
<input type="checkbox" name="myCheck[]" value="3">
<input type="checkbox" name="myCheck[]" value="4">