Generate Checkbox list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ward@nospam.net

    Generate Checkbox list

    Hello.

    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?

    Thanks.

    Ward
  • Erwin Moller

    #2
    Re: Generate Checkbox list

    ward@nospam.net wrote:
    [color=blue]
    > Hello.
    >
    > 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?
    >
    > Thanks.
    >
    > Ward[/color]

    Hi,

    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_gr oup = array();
    if (isset($_POST["sel_group"])){
    $selectedSel_gr oup = $_POST["sel_group"];
    }

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

    Security:
    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).

    Regards,
    Erwin Moller

    Comment

    • Jerry Stuckle

      #3
      Re: Generate Checkbox list

      ward@nospam.net wrote:[color=blue]
      > Hello.
      >
      > 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?
      >
      > Thanks.
      >
      > Ward[/color]

      Ward,

      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_arr ay()
      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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      Working...