logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ create a php dropdown list

Create A Php Dropdown List

by Admin _ April 23, 2022

Summary: in this tutorial, you will learn how lớn use the element khổng lồ create a drop-down list and a list box and how lớn get the selected values from the element in PHP.

Bạn đang xem: Create a php dropdown list

A quick introduction to lớn the element

The is an HTML element that provides a các mục of options. The following shows how to define a element in HTML:


label for="color">Background Color:label>select name="color" id="color">option value="">--- Choose a màu sắc ---option>option value="red">Redoption>option value="green">Greenoption>option value="blue">Blueoption>select>
Code language: HTML, XML (xml)The element has two important attributes:

id – the id associates the element with a elementname – the name attribute associates with the value for a size submission.

The element nested inside the element defines an option in the menu. Each option has a value attribute. The value attribute stores data submitted lớn the vps when it is selected.

If an option doesn’t have the value attribute, the value attribute defaults khổng lồ the text inside the element.

To select an option when the page loads for the first time, you can showroom the selected attribute to lớn the element.

The following example selects the Green option when the page first loads:


label for="color">Background Color:label>select name="color" id="color">option value="">--- Choose a màu sắc ---option>option value="red">Redoption>option value="green" selected>Greenoption>option value="blue">Blueoption>select>
Code language: HTML, XML (xml)

Getting the selected value from a element

We’ll create a khung that uses a element.

First, create the following folders & files:


├── css| └── style.css├── inc| ├── footer.php| ├── get.php| ├── header.php| └── post.php└── index.php
Code language: JavaScript (javascript)Second, place the following code in the header.php file:


Code language: HTML, XML (xml)Third, place the following code in the footer.php file:


Code language: HTML, XML (xml)Fourth, địa chỉ cửa hàng the following code to the get.php file to create a khung that has one element with a submit button:


form action="" method="post"> div> label for="color">Background Color:label> select name="color" id="color"> option value="">--- Choose a màu sắc ---option> option value="red">Redoption> option value="green" selected>Greenoption> option value="blue">Blueoption> select> div> div> button type="submit">Selectbutton> div>form>
Code language: HTML, XML (xml)The khung uses the POST method lớn submit data to the webserver.

Finally, địa chỉ cửa hàng the following code to lớn the post.php file:


$color = filter_input(INPUT_POST, "color", FILTER_SANITIZE_STRING);?> if ($color) : ?> p>You selected span style="color:"> echo $color ?>span>p> p>a href="index.php">Back khổng lồ the forma>p> else : ?> p>You did not select any colorp> endif ?>
Code language: HTML, XML (xml)To get the selected value of the element, you use the $_POST superglobal variable if the size method is POST & $_GET if the khung method is GET.

Alternatively, you can use the filter_input() function khổng lồ sanitize the selected value.

Xem thêm: Sửa Lỗi An Error Occurred While Reconnecting Z: To, An Error Occurred While Reconnecting

If you select the first option of the element, the selected value will be empty. Otherwise, the selected value is red, green, or blue.

Select with multiple options

To enable multiple selections, you địa chỉ the multiple attribute to lớn the element:


select name="colors<>" id="colors" multiple>...select>
Code language: HTML, XML (xml)When you select multiple options of a element & submit the form, the name will contain multiple values rather than a single value. To get multiple selected values, you địa chỉ cửa hàng the square brackets (<>) after the name of element.

Let’s take a look at an example of using a element with multiple selections.

First, create the following folders and files:


.├── css| └── style.css├── inc| ├── footer.php| ├── get.php| ├── header.php| └── post.php└── index.php
Code language: JavaScript (javascript)Second, place the following code into the header.php file:


Code language: HTML, XML (xml)Third, add the following code to the footer.php file:


Code language: HTML, XML (xml)Fourth, include the header.php and footer.php files in the index.php:


require __DIR__ . "/inc/header.php";$request_method = strtoupper($_SERVER<"REQUEST_METHOD">);if ($request_method === "GET") require __DIR__ . "/inc/get.php"; elseif ($request_method === "POST") require __DIR__ . "/inc/post.php";require __DIR__ . "/inc/footer.php";
Code language: HTML, XML (xml)If the HTTP request is GET, the index.php file will show a size from the get.php file. When the khung is submitted, the post.php tệp tin will handle the khung submission.

Fifth, create a size that contains a element with the multiple attribute in the get.php file. The name of the element has an opening và closing square bracket <> so that PHP can create an array that holds the select values.


form action="" method="post"> div> label for="colors">Background Color:label> select name="colors<>" id="colors" multiple> option value="red">Redoption> option value="green">Greenoption> option value="blue">Blueoption> option value="purple">Purpleoption> option value="magenta">Magentaoption> option value="cyan">Cyanoption> select> div> div> button type="submit">Submitbutton> div>form>
Code language: HTML, XML (xml)Finally, handle the khung submission in the post.php file:


$selected_colors = filter_input(INPUT_POST,"colors",FILTER_SANITIZE_STRING,FILTER_REQUIRE_ARRAY);?> if ($selected_colors) : ?>p>You selected the following colors:p>ul> foreach ($selected_colors as $color) : ?>li style="color:"> echo $color ?>li> endforeach ?>ul>p>p> else : ?>p>You did not select any color.p> endif ?>a href="index.php">Back lớn the forma>
Code language: HTML, XML (xml)The post.php tệp tin uses the filter_input() function lớn get the selected colors as an array. If you select one or more colors, the post.php file will display them.

Summary

Use the element lớn create a dropdown list.Use the multiple attribute to create a danh mục that allows multiple selections.Use $_POST to lớn get the selected value of the select element if the size method is POST (or $_GET if the form method is GET).Add square brackets(<>) after the name of the element to get multiple selected values.
Did you find this tutorial useful?
Yes No
*

Previously
PHP Radio Button
Up Next
PHP CSRF
*

search for:

Getting Started


PHP Fundamentals


Data Types


Operators


Control Flow


Functions


Arrays


Sorting Arrays


Advanced Functions


Advanced Array Operations


Forms


PHP Login


File I/O


String Opearations


About darkedeneurope.com


darkedeneurope.com helps you learn PHP programming from scratch.

Recent PHP Tutorials


Site Links


Share Tweet Linkedin Pinterest
Previous Post

Stdclass object php

Next Post

Php arrays aren't really arrays

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
jquery ajax post method

Jquery ajax post method

23/05/2022
browse /xampp windows/5

Browse /xampp windows/5

23/05/2022
top 17 date_default_timezone_set('asia/ho_chi_minh') hay nhất 2022

Top 17 date_default_timezone_set('asia/ho_chi_minh') hay nhất 2022

23/05/2022
how to install the lamp stack on centos 7

How to install the lamp stack on centos 7

21/05/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Nvidia là gì

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.