Check Box hell PHP

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
silver_fox4me
Forum Newbie
Posts: 1
Joined: Sun Jan 27, 2008 11:36 am

Check Box hell PHP

Post by silver_fox4me »

Hi, trying to load items from MySQL and then display them in a table with a check box to select the $pupil_id so i can either
multiple delete or go to a screen to view the displayed row.

So, far got the table printed out with the mysql data but cannot get the check box values to pass over to the next screen when i POST. Pretty sure its just the syntax regarding the checkbox part inside the $display_block

here's the code and a pic to show how the table and checkbox are done

$display_block .= "<table width='740' border='5' cellspacing='2'>";
$display_block .= "<form name='myform' action='post_check.php' method='POST'>";

while($row=mysql_fetch_array($results))
{

$pupil_id =$row['pupil_id'];
$pupil_first_name =$row['pupil_first_name'];
$pupil_last_name =$row['pupil_last_name'];
$pupil_gender =$row['pupil_gender'];
$pupil_year_group=$row['pupil_year_group'];
$display_block .="<tr><td width='50'>
<input type=checkbox name='box[]' value='$pupil_id'></td>

<td width='100'>$pupil_id</td>
<td width='100'>$pupil_gender</td>
<td width='200'>$pupil_last_name</td>
<td width='200'>$pupil_first_name</td>
<td width='100'>$pupil_year_group</td></tr>";


}

echo $display_block;
echo "<input type='submit' value='Submit' name='testform'>";
echo "</form";
echo "</table";


?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Check Box hell PHP

Post by califdon »

silver_fox4me wrote:

Code: Select all

<input type=checkbox name='box[]' value='$pupil_id'></td>
I don't think you can include characters like square brackets in a literal name for an HTML element. Clearly it won't render any kind of an array. You need to use PHP to generate unique element names, something more like this:

Code: Select all

$display_block .="<tr><td width='50'>
<input type=checkbox name='ID".$pupil_id."' value='$pupil_id'></td>
<td width='100'>$pupil_id</td>
<td width='100'>$pupil_gender</td>
<td width='200'>$pupil_last_name</td>
<td width='200'>$pupil_first_name</td>
<td width='100'>$pupil_year_group</td></tr>";
That would give each checkbox a name like "ID123" or whatever, so your POST variables could be stepped through and you would know which ID(s) had been checked.

For your future guidance, please use the Code button in the forum posting data entry screen, to make your code easier to read. Thanks.
Post Reply