Jump to content

[SOLVED] Variable assigned to a checkbox


nitation

Recommended Posts

Hello,

 

I have populated record from my database to a table.

 

This is my question, How do i have a checkbox for every record retrieved from my database and each checkbox should represent each record respectively.

 

To break it down:

 

i have this in a table:

 

ID          RecordName            Recordcheckbox

-----------------------------------------------

1            Business                  how do i add it (the value of this checkbox should be "Business")

2            System                    how do i add it (the value of this checkbox should be "System")

3            School                    how to i add it. (the value of this checkbox should be "School")

 

I hope my question is straight.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/120862-solved-variable-assigned-to-a-checkbox/
Share on other sites

i typically append the record id to the end of the checkbox NAME, for instance:

 

$sql = "SELECT ID, RecordName FROM yourTable ORDER BY ID";
$result = mysql_query($sql) or die(mysql_error());
while (list($rec_id, $rec_name) = mysql_fetch_row($result)) {
    echo "<INPUT TYPE='checkbox' NAME='checkbox$rec_id' VALUE='1'> $rec_name<BR>";
}

 

on submit, i'd do a similar thing, looping over id's to see if checkbox(id) is checked.

reading my solution, i realize there is probably a simpler solution: use the same checkbox name, but set the VALUE to the ID of each record:

 

$sql = "SELECT ID, RecordName FROM yourTable ORDER BY ID";
$result = mysql_query($sql) or die(mysql_error());
while (list($rec_id, $rec_name) = mysql_fetch_row($result)) {
    echo "<INPUT TYPE='checkbox' NAME='selectedRecords' VALUE='$rec_id'> $rec_name<BR>";
}

 

then loop over the $_POST['selectedRecords'] array to get each selected ID. (make sure $_POST['selectedRecords'] is an array before trying foreach).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.