To get multiple selected values, you add the square brackets ( ) after the name of element. When you select multiple options of a element and submit the form, the name will contain multiple values rather than a single value. To enable multiple selections, you add the multiple attribute to the element: Otherwise, the selected value is red, green, or blue. If you select the first option of the element, the selected value will be empty. To get the selected value of the element, you use the $_POST superglobal variable if the form method is POST and $_GET if the form method is GET.Īlternatively, you can use the filter_input() function to sanitize the selected value. The form uses the POST method to submit data to the webserver.įinally, add the following code to the post.php file: You selected "> Back to the form You did not select any color Code language: HTML, XML ( xml ) Third, place the following code in the footer.php file: Code language: HTML, XML ( xml )įourth, add the following code to the get.php file to create a form that has one element with a submit button: " method= "post"> Background Color: - Choose a color - Red Green Blue Select Code language: HTML, XML ( xml ) Second, place the following code in the header.php file: PHP select option Code language: HTML, XML ( xml ) We’ll create a form that uses a element.įirst, create the following folders and files: ├── css The following example selects the Green option when the page first loads: Background Color: - Choose a color - Red Green Blue Code language: HTML, XML ( xml ) Getting the selected value from a element To select an option when the page loads for the first time, you can add the selected attribute to the element. If an option doesn’t have the value attribute, the value attribute defaults to the text inside the element. The value attribute stores data submitted to the server when it is selected. The element nested inside the element defines an option in the menu. name – the name attribute associates with the value for a form submission.id – the id associates the element with a element.The element has two important attributes: The following shows how to define a element in HTML: Background Color: - Choose a color - Red Green Blue Code language: HTML, XML ( xml ) The is an HTML element that provides a list of options. Summary: in this tutorial, you will learn how to use the element to create a drop-down list and a list box and how to get the selected values from the element in PHP.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |