I use select
as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select
option and store it into a variable for future use, in PHP?
I use select
as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select
option and store it into a variable for future use, in PHP?
Depends on if the form that the select is contained in has the method set to "get" or "post".
If
<form method="get">
then the value of the select will be located in the super global array$_GET['taskOption']
.If
<form method="post">
then the value of the select will be located in the super global array$_POST['taskOption']
.To store it into a variable you would:
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
You can access values in the
$_POST
array by their key. $_POST is an associative array, so to accesstaskOption
you would use$_POST['taskOption'];
.Make sure to check if it exists in the $_POST array before proceeding though.
process.php
try this
Use this way:
But it is always better to give values to your
<option>
tags.Like this:
The index of the
$_POST
array is always based on the value of thename
attribute of any HTML input.