Home > Article > Backend Development > How to handle multi-level selection and display in PHP forms
How to handle multi-level selection and display in PHP forms
When developing web applications, forms are an integral part. Forms allow users to enter and submit data, allowing us to process and save user input. Sometimes, our forms will contain multiple levels of selections, such as selections for provinces and cities, or selections for multi-level classifications. In this article, I will introduce techniques on how to handle multi-level selection and display in PHP forms, and provide corresponding code examples.
First, we need to design corresponding tables in the database to store multi-level selected data. Taking provinces and cities as an example, we can design three tables: "provinces", "cities" and "districts". Each table contains an auto-incrementing primary key field that uniquely identifies each record, and a corresponding name field for display purposes.
For example, the "provinces" table can contain the following fields:
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255)
The "cities" table can contain the following fields:
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), province_id INT
The "districts" table can contain the following fields:
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), city_id INT
Next, we need to query the corresponding data based on the user's selection. In this example, we can use drop-down boxes to achieve multi-level selection. After the user selects a province, we need to query the corresponding city data based on the province's ID. Then after the user selects a city, we need to query the corresponding district and county data based on the city's ID.
The following is a simple database query code example:
<?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("数据库连接失败: " . $conn->connect_error); } // 获取省份数据 $sql = "SELECT * FROM provinces"; $result = $conn->query($sql); $provinces = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $provinces[$row['id']] = $row['name']; } } // 获取城市数据 $provinceId = $_POST['province_id']; $sql = "SELECT * FROM cities WHERE province_id = " . $provinceId; $result = $conn->query($sql); $cities = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $cities[$row['id']] = $row['name']; } } // 获取区县数据 $cityId = $_POST['city_id']; $sql = "SELECT * FROM districts WHERE city_id = " . $cityId; $result = $conn->query($sql); $districts = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $districts[$row['id']] = $row['name']; } } $conn->close(); ?>
Finally, we need to display the queried data in form and process user-submitted data. The following is a simple form display and processing code example:
<!DOCTYPE html> <html> <head> <title>多层级选择示例</title> </head> <body> <form method="post" action="process.php"> <label for="province">省份:</label> <select id="province" name="province_id"> <?php foreach ($provinces as $id => $name) { ?> <option value="<?php echo $id; ?>"><?php echo $name; ?></option> <?php } ?> </select><br> <label for="city">城市:</label> <select id="city" name="city_id"> <?php foreach ($cities as $id => $name) { ?> <option value="<?php echo $id; ?>"><?php echo $name; ?></option> <?php } ?> </select><br> <label for="district">区县:</label> <select id="district" name="district_id"> <?php foreach ($districts as $id => $name) { ?> <option value="<?php echo $id; ?>"><?php echo $name; ?></option> <?php } ?> </select><br> <input type="submit" value="提交"> </form> </body> </html>
In the above code, we use a foreach loop to display the queried data as drop-down box options. After the user selects the corresponding option, the form data will be sent to the "process.php" script that processes data when submitted. You can perform corresponding data processing and saving operations in this script.
This is how to handle multi-level selection and display in PHP forms. By properly designing the database and querying the data, we can easily implement multi-level selection functions and perform further operations through user-submitted data. Hope this article can be helpful to you!
The above is the detailed content of How to handle multi-level selection and display in PHP forms. For more information, please follow other related articles on the PHP Chinese website!