Home  >  Article  >  Backend Development  >  Create drop-down list with PHP

Create drop-down list with PHP

PHPz
PHPzforward
2024-03-01 08:46:051066browse

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.


Create a dropdown list in php

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.

  1. Static drop-down list
  2. Dynamic drop-down list

Let's take a look at the static dropdown list first.


Create a static dropdown list in PHP

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.

  1. PHP
  2. python
  3. Java
  4. c

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[&#39;Language&#39;])) {
echo "Selected Language: ".htmlspecialchars($_POST[&#39;Language&#39;]);
}
?>

The dropdown box should look like this.

PHP 创建下拉列表

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.

PHP 选择

This is how you create a dropdown box without a database connection. Now let's take a look at the dynamic dropdown list.


Create a dynamic dropdown list in PHP

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.

Create drop-down list with PHP

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 = &#39;root&#39;;
$pass = &#39;&#39;;
$db = &#39;sample tutorial&#39;;

$con = mysqli_connect("localhost", $user, $pass, $db);

$sql = "SELEC&#84;`BrandName` FROM `parkinglot1` WHER&#69; 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=&#39;NEW&#39;>
<option value="">--- Select ---</option>

<?php

while ($cat = mysqli_fetch_array(
$car_brands,MYSQLI_ASSOC)):;

?>
<option value="<?php echo $cat[&#39;BrandName&#39;];
?>">
 <?php echo $cat[&#39;BrandName&#39;];?>
</option>
<?php
endwhile;
?>
</select>
<input type="submit" name="Submit" value="Select" />
</form>
</body>
</html>

Output:

PHP 动态下拉框

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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete