SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Select Options

    I thought I posted this yesterday but can't find it. Who knows what happened to it.

    Anyway, I have a Form control with a Select element. I want to populate the options for the user to pick with a list from a database. How do I get this into the Select Options? Is there a relatively simple way with HTML or is this a question for PHP?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    html by itself cannot access the database to get the values for the dropdown

    you'll need a server-side language like php, yes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this should work. although after a while you may find it easier to write a function to do this.

    PHP Code:
    <select name="select">
    <?php
    $select
    =mysql_query("SELECT * FROM table");
    while( 
    $row=mysql_fetch_array($select) ){
    ?>
        <option value="<?php echo $row['value']; ?>"><?php echo $row['name']; ?></option>
    <?php
    }
    ?>
    </select>

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adam, thanks. I think that's exactly what I need to get started.
    My biggest question was how to get the data into the HTML.
    I have database skills, and programming experience, but PHP is new and something I'm working on.

    I may be trying to present too many data items, but it's a first step. Later, I may want to break this into two questions for the user - Product Category, then product selection. But for now I'll be happy to get a single product list working.

    Thanks again, your suggestion should get me going.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •