Using $_POST to get select option value from HTML

PhpHtmlArrays

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Use this way:

$selectOption = $_POST['taskOption'];

But it is always better to give values to your <option> tags.

<select name="taskOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

Solution 2 - Php

You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.

Make sure to check if it exists in the $_POST array before proceeding though.

<form method="post" action="process.php">
  <select name="taskOption">
    <option value="first">First</option>
    <option value="second">Second</option>
    <option value="third">Third</option>
  </select>
  <input type="submit" value="Submit the form"/>
</form>

process.php

<?php
   $option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
   if ($option) {
      echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
   } else {
     echo "task option is required";
     exit; 
   }

Solution 3 - Php

You can do it like this, too:

<?php
if(isset($_POST['select1'])){
    $select1 = $_POST['select1'];
    switch ($select1) {
	    case 'value1':
		    echo 'this is value1<br/>';
		    break;
	    case 'value2':
		    echo 'value2<br/>';
		    break;
	    default:
		    # code...
		    break;
    }
}
?>


<form action="" method="post">
    <select name="select1">
		<option value="value1">Value 1</option>
		<option value="value2">Value 2</option>
	</select>
    <input type="submit" name="submit" value="Go"/>
</form>

Solution 4 - Php

<select name="taskOption">
      <option value="first">First</option>
      <option value="second">Second</option>
      <option value="third">Third</option>
</select>

$var = $_POST['taskOption'];

Solution 5 - 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:

$option = $_POST['taskOption']

A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php

Solution 6 - Php

Like this:

<?php
  $option = $_POST['taskOption'];
?>

The index of the $_POST array is always based on the value of the name attribute of any HTML input.

Solution 7 - Php

<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>

try this

<?php 
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>

Solution 8 - Php


-- html file --

<select name='city[]'> 
				<option name='Kabul' value="Kabul" > Kabul </option>
				<option name='Herat' value='Herat' selected="selected">             Herat </option>
				<option name='Mazar' value='Mazar'>Mazar </option>
</select>

-- php file --

$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFelicia TanView Question on Stackoverflow
Solution 1 - PhpPraveen Kumar PurushothamanView Answer on Stackoverflow
Solution 2 - PhpRyanView Answer on Stackoverflow
Solution 3 - PhpDan CostinelView Answer on Stackoverflow
Solution 4 - PhpJeroen VannevelView Answer on Stackoverflow
Solution 5 - Phpuser2465080View Answer on Stackoverflow
Solution 6 - PhpJaime GrisView Answer on Stackoverflow
Solution 7 - PhpBulbul BigbossView Answer on Stackoverflow
Solution 8 - PhpLuckyView Answer on Stackoverflow