Help with Script - Need to work with Check Boxes

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
teparky
Forum Newbie
Posts: 1
Joined: Tue Nov 30, 2010 8:54 am

Help with Script - Need to work with Check Boxes

Post by teparky »

Hi All,

I am writing a Web Site that Reads some Data from a MySQL Database table and then shows it in a Table ina PHP Page. Once this data is displayed I want users to Tick boxes and then for the Page OR a Script to Read back in the Data that is ticked and then Write it to another Table in the SQL Database.

Here is the PHP To display the data from the First Table.
while($sSubject_row2 = mysql_fetch_array($result2))
{
if ($sSubject_row2['lilo'] == 'Outcome')
{

print'<tr><td>' .$sSubject_row2['UnitNumber'].'</td><td>'.$sSubject_row2['UnitName'].'</td><td>'.$sSubject_row2['lilo'].'</td><td>'.$sSubject_row2['Content'].'</td><td><input type="checkbox" name="check" id="check1"></td>';

}
}

Im just not quite sure how to write a script with Tick boxes and more than on tick box that may or may not be ticked.
Can someone please help me solve this issue.

Cheers
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Help with Script - Need to work with Check Boxes

Post by curlybracket »

It is the matter of correct name for checkboxes that will allow you to identify to which record each of them is bound. Instead of ordinary name for checkbox input, you have to add [] and make it an array:

Code: Select all

<td><input type="checkbox" name="check[checked]['.$sSubject_row2['id'].']"></td>
You also have to insert id of the record inside []. Now, after sending the form do print_r($_POST) and you will see how $_POST array looks now.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with Script - Need to work with Check Boxes

Post by califdon »

Before even thinking about the coding, my first question would be "Why are you doing this?" Every time I have ever seen a student reading data from a table and then writing (some of) it to another table in the same database, it's because the student doesn't understand databases. That's not something that is ever needed (now, someone will come along and think of some bizarre situation where it might be needed, I know, but certainly in 99.999% of cases, it is not needed).
Post Reply