简体   繁体   中英

Autofill dropdown menu based on value of another field in mysql php

I had two dropdown menu one is with many castes and other with community.. If i select a caste an equivalent community should be shown in the community drop down field. How should i do this?

In situations like this that value of second drop down depends on value of first drop down, you should use ajax for getting your data (in your case community).

lets assume that you have two tables in your database, one for caste and one for community. first you put castes in your first drop down and leave your second drop down empty. after user picked a caste you need to receive user choice and send that to your back-end through an ajax request. in back-end get your communities related to user chosen caste (with private-key and foreign-key) and send them to your front-end and put them in your empty second drop down.


Update: basically you create a new XMLHttpRequest object and then initialize a request with open() method (give your request method as first parameter, and your php file url as second parameter). Then you set a listener called onreadystatechange and inside its function you can get your server response with responseText property (but first check your requst state with readyState === 4). And finally send your request with send() method. put your parameters inside send() method like GET request parameters: "param1=value1&param2=value2". Just remember your server response is whatever you print in your php file (with echo, print_r, etc) if you don't print anything you'll get nothing in response. if you are ok with using Jquery library, it made ajax request easier. obviously ajax is more complicate than what I told you. I highly recommend you to take a look at some documents about ajax like this:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM