I have two drop down menus where the options are not get from the database.
The first one, lets the user to select a category.
<select name="category">
<option value="0">None</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>
The options of the second, are depended from the choice in the first dropdown menu. For example, if the user chooses the First option then the second dropdown will show
<select name="items">
<option value="3">Smartphone</option>
<option value="8">Charger</option>
</select>
but when the user change his mind, or select the second option first, then the second dropdown will now show
<select name="items">
<option value="1">Basketball</option>
<option value="4">Volleyball</option>
</select>
My question is how can I achieve this ? Can this be done without using a database?
Thank you!
See below to see the Working Example without using a Database.
Working Example Using MySQL Database
If you wanna connect it using Database, yeah, it is surely possible. Consider this table:
Table Structure
Initial HTML & PHP Code
Now, lets use PHP to first populate the initial
<select>:Now the
<select>is ready. With its onchange function, we can fire an AJAX event to get the new<select>with the data provided by the parent<select>.Now for the jQuery function, you can do this way:
In the HTML, after the
<select>, you need to give anotherselectwith anidassub.Processing PHP Source Code
Finally the source code of
process.php:Working Example without using a Database
You just need to replace this in the PHP.
And for the
process.php: