Follow me on Twitter to receive updates, free scripts and ongoing announcements!

PHP ajax cascading dropdown using MySql

Written by Codes Tips on May 16, 2009 – 5:31 pm -

To do cascading drop down in php using mysql we can use AJAX. The name AJAX is from asynchronous JavaScript and XML and it’s a new technique for web development to build rich interactive web application. The power of this technique comes from the fact that it can send to server requests from the client side, in order to not reload the page. The object that is using AJAX to send and retrieve data between server and the client is named XmlHttpRequest.
To build a cascading drop down in php to retrieve data from a mysql server we will first populate a drop down with the results from a first table from a mysql database. The first table is linked with a second table by a field. When we change the drop down we should assign to OnChanging event an Javascript function that will populate the second drop down . The function will create a new XmlHttpRequest object and make a call to other php script, grab data and populate the second drop down. The script that it’s called will fetch the results for that field that was changed.

Watch a demo here:populate drop down using ajax

Download php cascading drop down using mysql


Tags: , ,
Posted in AJAX, Codes, PHP/MYSQL, Scripts |

15 Comments to “PHP ajax cascading dropdown using MySql”

  1. jorgecoNo Gravatar Says:

    Please, verify download link.I got the message: Could not find anything!
    Sorry, but you are looking for something that isn’t here.

  2. Codes TipsNo Gravatar Says:

    Hei Jorgeco,
    It was a problem on security, please check again.

  3. jmellipseNo Gravatar Says:

    Hey,

    I used your example, I get data from the database for my second pull down. I can see all the values in it fine, but it doesn’t post any value when submitted. If I view the source in the browser there is only :

    Even tough the pulldown is filled ?!

    What can cause this ?

    Thanks

  4. jmellipseNo Gravatar Says:

    One more thing, what are you doing with the ‘nr’ variable ? I don’t see it after the while loop.

  5. Codes TipsNo Gravatar Says:

    Hei jmellipse,
    I can’t see the source that you posted it, can you re-post it?
    Are you saying that the second drop down doesn’t fill in the second drop down?

    The variable ‘nr’ it’s not used in code, i forgot it there.

    Cheers.

  6. shwetashahNo Gravatar Says:

    Hi there,

    You have really very nice post.
    But I require help in some other way. Can you please do me some favour..

    My requirement is,

    I have 1 textbox and 1 submit button. When I write some text into that textbox and click on submit button, related searches or say similar words will be fetched from database.

    Now upto here I am able to do, but problem is I want to show all those similar names as auto suggest list and then I select any name from it and it be displayed to that textbox. Its something like auto suggest but I want on click event.

    Not able to implement. :( I am in need.

    Thanks in advance,
    Shweta

  7. Codes TipsNo Gravatar Says:

    Hei Shweta,

    You can do with a ajax to fetch the results from database without refreshing the page when clicking the submit button.
    Then for every result that come from database build a list with tag < ui > and assign to every li elements a javascript onclick event to populate the textbox.

    Hope this helps.

  8. shwetashahNo Gravatar Says:

    Hey,

    Thanks a ton. It has helped me like coming out of cage aft long days. :)It worked.

    thnks

  9. fourmatNo Gravatar Says:

    Hi there, I am having the same problem that the other user had with missing input information.

    I can change the first field, and the AJAX script switched the second field just fine “visually”, the problem is that there is no data being recognized in the second field and it is not being passed as input to the script by the browser. This is most easily seen by setting the drop down boxes, then viewing the source. The source viewer only dispays the original information contained in the second box. This would not normally be a concern for me as the javascript switch may not bee seen by the source viewer, but for some reason the data is really not being passed.

    My recommendation is to set up your original demo script to actually submit the data and write it to the database. Any help would be greatly appreciated.

  10. fourmatNo Gravatar Says:

    IN looking at the code a little further, I think that the issue is that you are performing a change to the DIV with the AJAX function and not actually to the select input box. It appears that the script is essentially writing a second DIV box over top of the original, which is not being recognized as an actual change by the browser. I haven’t yet figured out how to correct this yet. Anyone care to chime in?

  11. fourmatNo Gravatar Says:

    UPDATE: Ok I fixed the issue. It was not with the formating of your script. I had an HTML problem where the tab was not placed correctly around the table where the input fields were. It would work in IE, but not in FF or Chrome. I fixed my HTML and everything works correctly.

  12. Codes TipsNo Gravatar Says:

    Hei fourmat, welcome to Codestips. I am glad you solved your problem, any questions please ask.

  13. Codes TipsNo Gravatar Says:

    Shweta, I am glad that I solved your problem, if you have other problems please post them.

  14. danieliserNo Gravatar Says:

    Hey.. im having an issue with this script. I am running it on xampp and am unable to get it to display in IE7, IE8 or FF. I can see it fine and it works as its supposed to displaying the results properly. I cant even get it to display after creating the tables you have in the files by default. Any ideas? do i need to enable something? thanks

  15. Codes TipsNo Gravatar Says:

    It’s working on IE6 or what?

Leave a Comment

You must be logged in to post a comment.