This example contains two select elements. When an item is selected in the first select, the choices in the other select are updated appropriately. The first select element contains a list of continents. The second select element will contain a partial list of countries located in the selected continent. There is an onchange event associated with the continent select. When the continent selection changes, the items in the country select are modified using JavaScript via the Document Object Model (DOM). All of the data required, the list of countries and continents, is included within the Web page. Show
Overview of the code below
Example Code:
Here is a working example: Dynamic Select Two dependent list boxes showing options of second list box based on selection of first list box. This cascading dropdown list box script is required in many places and there are two ways to handle such requirement. One is to get the full items list for both the drop down list to the client sides and manage by JavaScript and the other solution is submitting the form to the server on selection of first list and based on the selection get the element data for the second list. The first choice is advisable when we don't have many records for the second list and we can download all the data for the second list to client machine and handle it by using client side JavaScript. We will discuss the second solution as it is relevant to PHP MySQL section here and the first solution we will discuss in JavaScript drop down Tutorial section. You can see similar solution of managing second drop down based on the first drop down in ASP using VBScript Demo and sample scripts to download
Using PHP & AJax to manage dropdown list box
Other tutorials on Dropdown Listbox
Using jQueryIt became very easy to manage and modify the script if you are using JQuery. Today most of the sites uses JQuery for better user experience. Here are some tutorials with Demo for managing dropdown list box using Jquery. Here also we have used PHP and backend script and MySQL as database.
Two tables created in MySQL database for populating the data in the drop down lists. One is category table and other one is subcategory table. Both tables are linked by the field cat_id. The data taken for this example is common data of fruits, games, vehicles, colors used as category and corresponding values are taken as subcategory. The first table is populated in the first drop down list by taking directly from the table without any conditions in sql statement. In case of second drop down list the presence of variable having the value of category is checked and if the category number is present then the sql is changed to add one condition in where clause. The variable to second list query is passed by submitting the form to itself when any value is selected in the first drop down list. We will be using the onchange event of the select element to submit the form. We will write one function in JavaScript to handle the onchange event of the first drop down list box. We will keep this function inside our head tag. Once the element in first list gets selected it will call the function and the function will add the selected item to the query string and will reload the page. So for the second drop down list the variable will be available.
Please note that we have used dd.php as our file name. In your case change the file name. Now let us first prepare the Query for the first drop down list by getting the data from the category table. With this query we can populate the first drop down list. This is a simple solution but for the second drop down list we will check the presence of selected item of first drop down list. If it is
present then we will restrict our records to that category only other wise we will collect all the records for the second list. To know how the WHERE query works, you can visit our sql section. Here is the code to do this. Watch the if condition.As we are using variables taken from query string and using them inside our query, we must sanitize the variable before use for any malicious code or query which hackers can use. As we know our cat_id will have only numeric data so we will use is_numeric function to check the variable. You can read more on sql security here. This way we can control the data of the second list. Now the form
part we will work. See how the onchange event is used ( onchange=\"reload(this.form)\" ) inside the select command. This will execute the function reload and will submit the selected item to the page again by using the query string. MYSQLI & PDO to manage DatabaseHere in this example code we used MYSQLI functions to connect and manage MySQL database. In your downloaded ZIP file you will get different DEMO pages using with both MYSQLI and PDO to handle databaseHere is the code. If you are facing any problem in using this code, please post it in php discussion forum with your error messages, bugs etc. .. Dropdown list
without reloading the page by using Ajax & PHP plus2net.com
How do you get the selected value of dropdown in PHP without submit?php $selected_option=$_POST['option_value']; //Do what you need with this value, then echo what you want to be returned. echo "you have selected the option with value=$selected_option"; ?>
How do you get the selected value of dropdown in PHP without submit on same page in PHP?The only way you can get this code into PHP is by submitting the page. What you can do however is use javascript to get the value - and then fire off an AJAX request to a php script passing the selected value and then deal with the results, e.g.
How do I get the selected value of dropdown?The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
How get value from dropdown in database in PHP?How to Store Dropdown Value in Database in PHP.. Create SQL Database & Table.. Connect PHP to MySQL.. Create Input Field for Select Option.. Insert Select Option in Database.. Insert Select Option Value with another Input Value using PHP & MySQL.. |