Passing multiple value in select option

In select option we can’t use more than one value in option. To resolve this problem we can add more value in option value attribute with some seperator. After that we can split those value throught Javascript or Php. Here is some of the same code which i have done in my projects.

Spliting values using Javascript:

1
2
3
4
5
6
7
8
9
10
<select name="userdata" id="userdata">
	<option value="john,25,UK">john</option>
	<option value="Petter,20,US">petter</option>
	<option value="Ismal,25,Pak">ismal</option>
	<option value="Raj,28,Ind">raj</option>
	<option value="libby,25,UK">libby</option>
</select>
//Jquery code to get multiple value
var userData = $("#userdata").val(); // getting the selected value
userData = userData.split(','); // Spliting the value based on the seperator ","

Spliting values in PHP

In php we can split the word using a function called explode “array explode ( string $delimiter , string $string [, int $limit ] ) “

1
2
3
4
<?php
$userdata = $_POST["userdata"]; // gets the selected value
$userdata = explode(",", $userdata); // splits the value and stores the value in a array called $userdata[].
?>