Generate Checkbox list

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a table with two fields:
    1)  groups_id
    2)  groups_name

The data contained is simple:

    groups_id    groups_name
    -----------    ----------------
        1          Group A
        2          Group B
        3          Group C
        4          Group D

I would like to dynamically create a checkbox selection similar to:

<input name="sel_group" type="checkbox" value="1">Group A<BR>
<input name="sel_group" type="checkbox" value="2">Group B<BR>
<input name="sel_group" type="checkbox" value="3">Group C<BR>
<input name="sel_group" type="checkbox" value="4">Group D<BR>

So I need a script that'll take this information from the groups table
and create the above.  Can you help?



Re: Generate Checkbox list wrote:

Quoted text here. Click to load it


This is done easiest in a awkward way in PHP.
Just name your checkboxes sel_group[] istead of sel_group, and then in  
receiving script:

$selectedSel_group = array();
if (isset($_POST["sel_group"])){
  $selectedSel_group = $_POST["sel_group"];

Now you have them in the array $selectedSel_group. (Can be empty of course).

Also keep in mind that the content of the formfields can be something else  
than the numbers you provide when the visitor is a bad guy (SQL-injection  
and such tricks), so you have to make sure the contents of the array are  
indeed numbers (if that is what you expect).

Erwin Moller

Re: Generate Checkbox list wrote:
Quoted text here. Click to load it


Pretty simple, really.  Just SELECT the information from the table and  
use echo or print to display the results in the html.

You'll need to:
   1. Connect to the database - mysql_connect()
   2. Select a database - mysql_select_db()
   3. Query the database for the information - mysql_query()
   4. Fetch the results - mysql_fetch_array()
   5. Write the HTML for your input statement - echo
   6. Repeat 4 and 5 while you have more data
   7. Close the database - mysql_close()

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline