PHP script for SELECT OPTION FIELD:

HTML select tag allows user to choose one or more options from the given drop down list. Below example contains PHP script to get a single or multiple selected values from given HTML select tag. We are covering following operations on select option field using PHP script.

To get value of a selected option from select tag:


<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];  // Storing Selected Value In Variable
echo "You have selected :" .$selected_val;  // Displaying Selected Value
}
?>

 

To get value of multiple select option from select tag, name attribute in HTML <select> tag should be initialize with an array [ ]:


<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>

 

PHP script for RADIO BUTTON FIELD:

HTML <input type=”radio”> allows user to choose one option from the given choices. Below codes contains PHP script to get a selected value from given HTML <input type=”radio”>.

To get selected value of a radio button:


<form action="" method="post">
<input type="radio" name="radio" value="Radio 1">Radio 1
<input type="radio" name="radio" value="Radio 2">Radio 2
<input type="radio" name="radio" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radio']))
{
echo "You have selected :".$_POST['radio'];  //  Displaying Selected Value
}
?>

In below example, we have created a form having select tag and some radio buttons, As user submits it, Value of selected options will be displayed.

Watch our live demo or download our codes to use it.

Our complete HTML and PHP codes are given below.

PHP file: form.php
Given below our complete HTML contact form.


<!DOCTYPE html>
<html>
<head>
<title>PHP Get Value of Select Option and Radio Button</title> <!-- Include CSS File Here-->
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP Multiple Select Options and Radio Buttons</h2>
<form action="form.php" method="post">
<!----- Select Option Fields Starts Here ----->
<label class="heading">To Select Multiple Options Press ctrl+left click :</label>
<select multiple name="Color[]">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
<option value="White">White</option>
<option value="Black">Black</option>
<option value="Violet">Violet</option>
<option value="Limegreen">Limegreen</option>
<option value="Brown">Brown</option>
<option value="Orange">Orange</option>
</select>
<?php include'select_value.php'; ?>
<!---- Radio Button Starts Here ----->
<label class="heading">Radio Buttons :</label>
<input name="radio" type="radio" value="Radio 1">Radio 1
<input name="radio" type="radio" value="Radio 2">Radio 2
<input name="radio" type="radio" value="Radio 3">Radio 3
<input name="radio" type="radio" value="Radio 4">Radio 4
<?php include'radio_value.php'; ?>
<input name="submit" type="submit" value="Get Selected Values">
</form>
</div>
</div>
</body>
</html>

PHP file: select_value.php

To display multiple values, we used foreach loop here.


<?php
if(isset($_POST['submit'])){
if(!empty($_POST['Color'])) {
echo "<span>You have selected :</span><br/>";
// As output of $_POST['Color'] is an array we have to use Foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "<span><b>".$select."</b></span><br/>";
}
}
else { echo "<span>Please Select Atleast One Color.</span><br/>";}
}
?>

PHP file: radio_value.php

To display radio buttons value.


<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radio']))
{
echo "<span>You have selected :<b> ".$_POST['radio']."</b></span>";
}
else{ echo "<span>Please choose any radio button.</span>";}
}
?>

CSS File: style.css

Styling HTML elements.


@import "http://fonts.googleapis.com/css?family=Droid+Serif";
/* Above line is used for online google font */
div.container {
width:960px;
height:610px;
margin:50px auto;
font-family:'Droid Serif',serif
}
div.main {
width:308px;
float:left;
border-radius:5px;
border:2px solid #990;
padding:0 50px 20px
}
span {
color:red;
font-weight:700;
display:inline-block;
margin-bottom:10px
}
b {
color:green;
font-weight:700
}
h2 {
background-color:#FEFFED;
padding:25px;
margin:0 -50px;
text-align:center;
border-radius:5px 5px 0 0
}
hr {
margin:0 -50px;
border:0;
border-bottom:1px solid #ccc;
margin-bottom:25px
}
label {
color:#464646;
text-shadow:0 1px 0 #fff;
font-size:14px;
font-weight:700;
font-size:17px
}
select {
width:100%;
font-family:cursive;
font-size:16px;
background:#f5f5f5;
padding:10px;
border:1px solid
}
input[type=radio] {
margin-left:15px;
margin-top:10px
}
input[type=submit] {
padding:10px;
text-align:center;
font-size:18px;
background:linear-gradient(#ffbc00 5%,#ffdd7f 100%);
border:2px solid #e5a900;
color:#fff;
font-weight:700;
cursor:pointer;
width:100%;
border-radius:5px
}
input[type=submit]:hover {
background:linear-gradient(#ffdd7f 5%,#ffbc00 100%)
}

Conclusion:
Using these values you can perform other operations like CRUD (Create, Read, Update & Delete) in database. Hope you like it, keep reading our other blogs.