Home >Backend Development >PHP Tutorial >Create drop-down list with PHP
php editor Xiaoxin introduces you how to use PHP to create a drop-down list. Drop-down lists are common interactive elements in web pages. By dynamically generating drop-down options through PHP, more personalized functions can be achieved. In PHP, you can use a loop structure to traverse the array or database query results, fill the data into the drop-down list, and achieve the effect of dynamically loading options. Through simple code implementation, you can easily create drop-down lists that meet your needs, adding more interactivity and user-friendliness to web pages.
A drop-down list is a set of items in a list. The content is invisible until you click the little arrow.
This article will introduce two types of drop-down lists.
Let's take a look at the static dropdown list first.
A static dropdown is a simple PHP dropdown box without a database connection. We will create a static dropdown box for some Programming Language in the sample code below.
In the list we will have the following languages.
We will then use PHP to echo the
selected language.
Code:
//Create a static dropdown box <fORM id="L" method="post"> <select name="Language"> <option value="PHP">PHP</option> <option value="Python">Python</option> <option value="Java">Java</option> <option value="C++">C++</option> </select> <input type="submit" name="Submit" value="Submit"> </form> <?php if(isset($_POST['Language'])) { echo "Selected Language: ".htmlspecialchars($_POST['Language']); } ?>
The dropdown box should look like this.
We click on the arrow to display the full list of items in the drop-down box in the image above. Let’s try selecting language PHP
from the menu and see what happens.
This is how you create a dropdown box without a database connection. Now let's take a look at the dynamic dropdown list.
Dynamic dropdown gets content from database. Let's look at an example.
We have a Mysql
database called sample tutorial. In our database we have table parkinglot
.
See table below.
From the table above, we will create a dropdown box to get the contents of our BrandName
row.
First, we will create a database connection and use the SELECT * FROM
function to get the contents of the BrandName
row. Finally, we will create a drop-down menu for the above items.
Code:
<?php $user = 'root'; $pass = ''; $db = 'sample tutorial'; $con = mysqli_connect("localhost", $user, $pass, $db); $sql = "SELECT`BrandName` FROM `parkinglot1` WHERE 1;"; $car_brands = mysqli_query ($con, $sql); ?> <html> <head> <title>Dynamic Drop Down Box</title> </head> <BODY bGColor ="yellow"> <form id="form" name="form" method="post"> Car Brands: <select Brand Name='NEW'> <option value="">--- Select ---</option> <?php while ($cat = mysqli_fetch_array( $car_brands,MYSQLI_ASSOC)):; ?> <option value="<?php echo $cat['BrandName']; ?>"> <?php echo $cat['BrandName'];?> </option> <?php endwhile; ?> </select> <input type="submit" name="Submit" value="Select" /> </form> </body> </html>
Output:
The code is successful. We managed to get the contents of the table from the database and use them in the dropdown box.
This article shows how to create two drop-down list types in PHP.
The code for the dynamic drop down box does not execute when you select any car brand. It only shows what's in our database.
The above is the detailed content of Create drop-down list with PHP. For more information, please follow other related articles on the PHP Chinese website!