PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: multiple values
multiple values - phponwebsites.com
Showing posts with label multiple values. Show all posts

13 Dec 2015

Get multiple selected values from drop down box using PHP

                           You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Now the doubt is rise. How to select multiple values from drop down box and how to get multiple selected input data from drop down box in PHP. This post describes retrieve multiple selected values from drop down box using PHP.

How to select multiple values from drop down box in HTML


                           You know select single value in drop down box. Then how to select multiple values in drop down box. You have to add attribute "multiple" and set name as array in drop down box in order to select multiple values from drop down box. If you want to select more than one values in drop down box, then press "ctrl" while selecting values in drop down box. Let consider below example to make HTML formwith select multiple values in drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
</body>
</html>

Retrieve multiple selected values from drop down box using PHP


                                 You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Then display each selected items through foreach loop in PHP. The PHP script describes how to select multiple selected values from drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
<?php
 if(isset($_POST['submit']))
 {
  $country=$_POST['country'];
  foreach($country as $val)
  {
    echo $val.'<br>';
  }
 }
?>
</body>
</html>

               
                                      While submitting form with selected multiple values from drop down box, you can get multiple selected values using PHP.

13 May 2015

Get multiple values from checked checkboxes using PHP

                      Normally you can get single value from textbox, checkbox and radio buttons. Can you retrieve multiple values from selected checkbox.? Yes you can get multiple values from checked checkbox using PHP.

Get value from checked checkbox using PHP:


                         You can retrieve value from single checkbox using php and html. The following php code is used for get values from checked checkbox.

<html>
<body>
<?php
if(isset($_POST['submit']))
{
echo'You checked following value:<br>';
echo $_POST['check'];
}
else
{
?>
<form action="#" method="post">
<input type="checkbox" name="check" value="apple">apple <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
}
?>
</body>
</html>


Retrieve values from multiple checked checkboxes using php:


                       If you want to get multiple values from checked checkbox, then you use name as an array for your checkbox. It should be like following.

<input type="checkbox" name="check[]" value="apple">apple
<input type="checkbox" name="check[]" value="orange">orange
<input type="checkbox" name="check[]" value="mango">mango

The following php code is used to get values from multiple checked checkbox.


<html>
<body>
<?php
if(isset($_POST['submit']))
{
echo'You checked following value(s):<br>';
 foreach($_POST['check'] as $val)
 {
 echo $val.'<br>';
 }
}
else
{
?>
<form action="#" method="post">
<input type="checkbox" name="check[]" value="apple">apple <br>
<input type="checkbox" name="check[]" value="orange">orange <br>
<input type="checkbox" name="check[]" value="mango">mango <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
}
?>
</body>
</html>


     where ,
                  $_POST['check'] is used to get values from checkbox using php.
                  foreach() is used to get array values from checkbox and display them

When you run this php file, your output like as below:

get values from multiple check box

Suppose, you select check boxes like below:

get values from checked checkbox using php

You'll get output like as below

                        You checked following value(s):
                         apple
                         mango