Mixed Media and Web Development.

Chained Select Boxes using PHP / MySQL / AJAX

Chained Select Boxes using PHP / MySQL / AJAX

So, I got sick of using multiple singular select boxes. For this latest project I ended up with a table containing a category and text. About 500 entries. I used the AJAX from http://dhtmlgoodies.com

Source Files: DOWNLOAD
Check out the DEMO

The MySQL is simple, just an id, category and text field. There are multiple of the same category so I will start with the mysql connection and the query to pull them into a select using distinct.

dbcommon.php

 	mysql_connect("localhost", "username", "password") or die(mysql_error());
	mysql_select_db("databasename") or die(mysql_error());

Select Boxes.

main select box query

$result = mysql_query("SELECT DISTINCT category FROM dialogue")
			or die(mysql_error());

			while($cat = mysql_fetch_array( $result )) 

			{        //this syntax highlighter blows, there should be option tags in this echo.
				  echo '';
			}

JS within the select box page.



getText.php

include('dbcommon.php');

    $category = $_GET['category'];

	if(isset($_GET['category'])){

		$result = mysql_query("SELECT * FROM dialogue WHERE category='$category'")
	    or die(mysql_error());

			 while($text = mysql_fetch_array( $result )) 

			  {
				echo 'obj.options[obj.options.length] = new Option("'.$text['text'].'","'.$text['id'].'");';
			  } 

	}

Im not much for tutorials but you can see the way I did it.



22 Responses to “Chained Select Boxes using PHP / MySQL / AJAX”

  1. Samuel says:

    Hi Rob,
    Got the script working correctly, thanks a million…I’m wondering how to post the value of the second select to an Insert/Update form function? Is this possible?
    Cheers
    Samuel

  2. Roger says:

    Hi Rob,

    So my next question is,
    How do you send the result of the second as a search term to a search page..
    Presuming this can be done

  3. Roger says:

    Rob

    Many Tanks

    I got it working

    ;)

  4. Roger says:

    Hmm doesn’t seem to display it all

    link: http://www.179a.com/Roger.txt

  5. Roger says:

    Hi Rob

    here is my form

    =====================================

    Select a Category
    <?php

    $result = mysql_query(”SELECT DISTINCT artist FROM mp_id3_tags”)
    or die(mysql_error());

    while($cat = mysql_fetch_array( $result ))

    {
    echo ”.$cat['artist'].”;
    }

    ?>

  6. Roger says:

    Hi Rob

    here is my form

    =====================================

    Select a Category
    <?php

    $result = mysql_query(”SELECT DISTINCT artist FROM mp_id3_tags”)
    or die(mysql_error());

    while($cat = mysql_fetch_array( $result ))

    {
    echo ”.$cat['artist'].”;
    }

    ?>

    ========================================
    Here is my gettext.php
    ===========================================

  7. Rob says:

    post the code you have and i can help

  8. brizal08 says:

    hey bob i have a problem on database there’s something wrong regarding with the fields and tables

    the Table requires me ‘updata.metro’ doesn’t exist

    because my table here is “location” and my field is “Metro Manila” & “Province”

    please help me ASAP

  9. Rob says:

    which part is not working, can you post what you have done.

  10. Roger says:

    Well I tried doing that , but I just can’t get it

  11. Rob says:

    Change everything called category to artist and everything called text to album.

    in the form where you see this..

    is where the php goes, under the form snippet. that populates the form.

  12. Roger says:

    Yes your right, I have them in the same table..

    so how do it now

    Roger

  13. Rob says:

    you should have the album and artists in the same table

  14. Roger says:

    WOW this is just what I was looking for….

    I have a table with `artist` and one with `album` how would I change this to show Artist in first and Album in the second,
    Really would appreciate any help that could be given.
    Note: I spent 2 hrs trying to solve this myself, without any luck…

  15. Rob says:

    @Bryan

    2 tables, one is just categories the other is categories and the text for the seconds dropdown. its just matching categories.

  16. Bryan says:

    In regards to my last post, I’m having trouble populating the second select box once something was chosen from the first select box.

    About the db, are there various tables? How are the select boxes linked to each other inside the db?

    Regards,
    Bryan

  17. Bryan says:

    Rob,

    How did you organize your db? I can’t seem to figure out tying my db with the code you have provided.

  18. Bryan says:

    Very cool, Rob. After I posted the question, I found out it could be done with the visibility: hidden style.

    Thanks

  19. Rob says:

    @Bryan

    The sky is the limit on this one. What I have provided is a simple example. Ive added in the hide/show portion.

  20. Bryan says:

    How many child-trees can there be off of the parent?

    Is there a way to completely hide the select boxes that come after the first one? In the demo there are two select boxes but the second one has nothing in it. Would it be possible to make the select boxes appear as needed?

    Regards,
    Bryan

  21. Jay says:

    This came in really handy and after some tweaking works perfectly for what I needed to do. Thanks for making this available…it saved me loads of time!

  22. Ryan says:

    Very cool, implemented this into a site I’m developing :) Had to make a few changes as my categories and sub-categories are stored in seperate tables in the DB.

    I’ve also set it to pre-select whatever category is currently being viewed, but having a little difficulty pre-selecting the sub-category. Gonna stick at it a while longer and will post up my code when working if anyone wants it.

    Thanks for the initial code, has certainly helped.

Leave a Reply