Checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Checkboxes

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Post 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:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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">
Post Reply