如何在 PHP 中獲取選定的選項值
在本文中,我們將瞭解如何在 PHP 中獲取選定的選項值,並附帶必要的示例。
什麼是 PHP 中的“選項值”?
下拉選單是使用 <select> 和 <option 標籤> 建立的。PHP 允許使用者從列表中選擇一個或多個選項。
語法
以下是 PHP 中選項值的語法:
<select name = "select_list_name" size ="n" multiple> <option value ="choice-name 1" selected>Text Label-1</option> <option value ="choice-name 2" selected>Text Label-2</option> . . . </select>
與 <select> 標籤相關的特性包括:
- Multiple: 用於從列表中選擇多個選項。
- Name: 指定下拉列表的名稱。
與 <option> 標籤一起使用的特性包括:
- Value: 指定提交表單時傳送的值。
- Selected: 用於指定表單首次在瀏覽器中載入時預選的選項。
讓我們看看如何在 PHP 中獲取選定選項值的幾個不同示例:
示例 1
這是一個示例,展示如何在 PHP 中獲取選定的選項值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>PHP Dropdown Selection</title> <style> .wrapper { max-width: 450px; margin: 70px auto; text-align: center; } input[type="submit"] { margin-bottom: 20px; } .dropdown-container { width: 370px; margin: 90px auto 30px; position: relative; } select { width: 100%; height: 55px; font-size: 16px; font-weight: bold; cursor: pointer; border-radius: 5px; background-color: #ff5733; border: none; color: white; padding: 10px 40px 12px 15px; appearance: none; transition: color 0.3s ease, background-color 0.3s ease; } select:hover { color: #333; background-color: #fff; } select:focus { color: #333; background-color: #fff; } h2 { font-style: italic; font-family: "Georgia", serif; color: #666; font-size: 1.8em; font-weight: bold; } h1 { font-family: "Georgia", serif; color: #444; font-size: 2.5em; font-weight: bold; } input[type=submit] { border: 2px solid #ff5733; border-radius: 5px; background-color: #fff; color: #ff5733; font-size: 14px; font-weight: bold; padding: 10px 30px; cursor: pointer; transition: all 0.3s ease; } input[type=submit]:hover { background-color: #ff5733; color: white; } </style> </head> <body> <div class="wrapper"> <h1>Movie Picker</h1> <h2>Select Your Favorite Movie</h2> <form action="" method="post"> <select name="movieList"> <option value="" selected>Choose a movie</option> <option value="inception">Inception</option> <option value="titanic">Titanic</option> <option value="interstellar">Interstellar</option> <option value="gladiator">Gladiator</option> <option value="avatar">Avatar</option> </select> <br><br> <input type="submit" name="submitButton" value="Submit"> </form> <?php if (isset($_POST['submitButton'])) { if (!empty($_POST['movieList'])) { $chosenMovie = $_POST['movieList']; echo '<p>You selected: <strong>' . ucfirst($chosenMovie) . '</strong></p>'; } else { echo '<p style="color: red;">Please select a movie from the list.</p>'; } } ?> </div> </body> </html>
輸出
以下是以下程式碼的結果:
此程式碼生成一個基本的網頁,其中包含一個下拉選單,允許使用者從列表中選擇一部電影。選擇一部電影並點選提交後,所選電影的標題就會顯示出來。如果使用者沒有選擇電影,則會顯示一條錯誤訊息,要求使用者選擇電影。
示例 2
現在我們將編寫一個 PHP 程式碼,說明如何從下拉列表中獲取選定的多個選項值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>PHP Multi-Select Dropdown Example</title> <style> h2 { font-family: Arial, sans-serif; color: #555; font-size: 1.5em; margin-bottom: 15px; } h1 { font-family: Arial, sans-serif; color: #333; font-size: 2.5em; margin-bottom: 10px; }.container { max-width: 500px; margin: 50px auto; text-align: center; }.dropdown-container { margin: 20px 0; } select { width: 100%; height: 150px; font-size: 14px; font-family: Arial, sans-serif; border: 1px solid #888; border-radius: 5px; padding: 10px; background-color: #f8f9fa; color: #333; cursor: pointer; } select:hover { border-color: #007bff; } select:focus { outline: none; border-color: #007bff; background-color: #fff; } input[type="submit"] { background-color: #007bff; color: white; border: none; padding: 10px 20px; font-size: 16px; font-family: Arial, sans-serif; border-radius: 5px; cursor: pointer; } input[type="submit"]:hover { background-color: #0056b3; } .message { font-family: Arial, sans-serif; color: #333; margin-top: 20px; font-size: 16px; } .message.error { color: red; } </style> </head> <body> <div class="container"> <h1>Subject Selector</h1> <h2>Select Your Subjects</h2> <form method="post"> <div class="dropdown-container"> <select name="subjectChoices[]" multiple size="6"> <option value="data_structures">Data Structures</option> <option value="operating_systems">Operating Systems</option> <option value="physics">Physics</option> <option value="calculus">Calculus</option> <option value="ai">Artificial Intelligence</option> <option value="ml">Machine Learning</option> </select> </div> <input type="submit" name="submitButton" value="Submit"> </form> <?php if (isset($_POST["submitButton"])) { if (isset($_POST["subjectChoices"])) { echo '<div class="message">'; foreach ($_POST['subjectChoices'] as $selectedSubject) { echo "You selected: " . ucfirst(str_replace("_", " ", $selectedSubject)) . "<br/>"; } echo '</div>'; } else { echo '<div class="message error">Please select at least one subject!</div>'; } } ?> </div> </body> </html>
輸出
這將生成以下輸出:

此程式碼使用 HTML 和 PHP 建立了一個多選下拉表單,允許使用者選擇多個科目並提交他們的選擇。提交後,頁面上將使用 PHP 顯示選定的科目,如果沒有選擇任何選項,則會顯示一條錯誤訊息。它具有下拉選單和按鈕,旨在易於使用。
廣告