Very simply, I have one dropdown menu dynamically populated with data:
SQL Code
$querycourse = "SELECT course, COUNT(course) AS count FROM acme WHERE course IS NOT NULL GROUP BY course ";
$procc = mysqli_prepare($link, $querycourse);
$queryc = mysqli_query($link, $querycourse) or die(mysqli_error($link));
PHP Code
echo "<select name='course[]' value='' multiple='multiple' size=10>";
// printing the list box select command
echo "<option value=''>All</option>";
while($ntc=mysqli_fetch_array($queryc)){//Array or records stored in $nt
echo "<option value=\"$ntc[course]\">\"$ntc[course]\"</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
What I need is another dropdown that is populated with data based on a selection from the first dropdown box.
I am using MySQL, PHP, Javascript and can also (at a push) use jQuery. I have no experience in Ajax.
Would anyone be kind enough to point me in the right direction?!
Thanks in advance, as always,
Homer.
First and Best Method (If you have or may have enough option specific data)
Use AJAX. It is the easiest way, I think, compared to the other ways to implement the same. Use Jquery to implement AJAX. It makes AJAX a piece of cake! Here I share my piece of cake for you -
Following is roughly the complete code you need -
Call a javascript function populateSecondDropdown() on your first select like this -
Define an ajax call inside inside the populateSecondDropdown() function -
And finally the query to fetch 2nd dropdown's options in the AJAX processor file fetchOptions.php. You can use $_POST['id_option'] here to fetch the options under it. The database query here should fetch the
option_id
andoption_name
fields for every option (as expected by the jquery code inside$.each
) and return a json encoded array like this:-Second Method (Using only javascript)
Fetch all the data for the second dropdown grouped by the field of the first dropdown. E.g. let's take courses displayed in the first dropdown and subjects under courses displayed in the 2nd
Create all the options of the 2nd dropdown. Assign classes equal to the courses while creating the options like this:-
Then define
onchange="displaySubjectsUnderThisCourse(this);"
for the first dropdown and write this javascript :-The basic idea here is to hide/display option groups but my code may have errors.
Finally, please note, the second method (fetching all the option values) would be better only if you have limited small amount of data and are very sure there will always be less data in future. But, since nobody can be so certain about the future, it is advisable to use the AJAX method always.
There are two methods:
The first option is very fast and easy, but may take a while to load if you have a large list of options for the second select.
The second option is more complicated, but much more flexible.
Here's some Ajax code to get you started:
Create a request:
Send the request:
Use the results:
How you handle the server side portion is up to you.