Results 1 to 6 of 6

Thread: Dropdown box that pulls from MYSQL database

  1. #1
    Join Date
    Aug 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dropdown box that pulls from MYSQL database

    hi there

    I need to create a "refine your search" function on a php page which consists of three dropdown boxes (box 1:brand, box 2: price, box 3:country) and when the user presses the "GO" button the correct products are pulled from corresponding fields in my MYSQL database and outputted onto the page.

    The dropdown boxes do not need to populate from the database - they are static.

    My php is basic, but I can write php functions to pull from my database onto a page. I think I might need some other scripts, such as JavaScript to do this too..???

    My problem is pulling it all together and knowing where to start. I can't get my head around the fact that normally, your php page instructs to open your database, pull from it, and the products are returned on the page once it has loaded for the user, without any of the php instructions visible in the source code. With a dropdown, I don't understand how it all comes together, as the php instruction to pull from the databse will not take place until the user presses the "GO" button! Help - v confused!

    Calling experienced coders....

    Can anyone help, or point me in the direction of a tutorial (not an overview of php or similar) that goes through the process?

    Many thanks in advance

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    A quick search doesn't reveal anything so I'll try just explaining it...

    When a browser (or, more generally, a client) requests a page from a server, some information is sent about exactly what is being requested. This includes the page name and optionally some variables. In a form submission, these variables are sent one of two ways, depending on the <form method> attribute, whether it is "get" or "post" (capitalized in the HTTP specification but not as HTML attribute values).

    In a GET request, the variables are sent in the URL; this page has the GET variable 't' with a value of 46104. (If no variables are sent, the request is GET.) POST requests are almost identical to GET from a client's or server's perspective; the only real difference is that POST variables are normally hidden from the user. This is useful if the request is not idempotent (i.e., doing more than just looking) or the variables contain sensitive information; most effects should not be preserved on the clipboard or in bookmarks for later use. Simple database queries (e.g., Google searches) are alright using GET, and this makes them much more useful than POST would.

    So here's what happens in your scenario:
    1. The client GET requests the ordinary form page.
    2. The server processes any PHP (or other server-side language) in the requested file and sends the result.
    3. The client submits the form by:
      • requesting the page specified in the form's 'action' attribute
      • with the variables from the form elements
      • using the request type specified in the form's 'method' attribute.
    4. The server processes any PHP (or other server-side language) in the requested file and sends the result.


    The part of all this which I think is confusing you is that your form's 'action' is the form page itself. This is a very common practice; the two pages are actually different because of the sent variables. If the form is submitted with GET, this will be more obvious. The coincidence is just a handy way of maintaining an HTML structure and some very similar PHP programs in only one file instead of two or more.

    Ask questions...
    Last edited by Jesdisciple; 06-22-2009 at 05:56 AM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Here's something someone else showed me in relation to his own problem today. It doesn't require AJAX, and I've made sure it works.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  4. #4
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think that if you start the select then enter into a while loop then close it out that would work...
    PHP Code:
    function builddropdown($connectionType$connectionName$connectionPass$name$dbTable$dbField){
        echo
    "<select name=$name >\n";
        
    $con mysql_connect($connectionType$connectionName$connectionPass);
        if (!
    $con)
          {
              die(
    'Could not connect: ' mysql_error());
          }
        
    mysql_select_db("$dbName"$con);
        
    $result mysql_query("SELECT * FROM $dbTable GROUP BY $dbField HAVING 0 < count( $dbField ) ORDER BY $dbField ASC");
        while(
    $row mysql_fetch_array($result))
          {       
               echo 
    "<option value=".$row['FieldName']." >".$row['FieldName']."</option>\n";
          }
        echo 
    "</select>\n";

    just call this function in your form.

  5. #5
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Hi Elki. Sorry that I threw you off with that link; only the thread titles are similar between this and the one I found that on. The problem is completely different, and your post is just as off-topic as my second one.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  6. #6
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Doh, sorry.

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
  •