Programmers learn & share
62 views

Solution:

1. For Windows – Hold down + Ctrl key for selecting multiple option

2. For Mac – Hold down command key for selecting multiple option

STEP 1: Create a list of item

Code:

<html> 
	<body> 
		<form method = "post" action = "name.php"> 
			<h4>SELECT SUJECTS</h4> 
			<!--Using multiple to select multiple value-->
			<select name = "subject" multiple size = 6> 
				<option value = "english">ENGLISH</option> 
				<option value = "maths">MATHS</option> 
				<option value = "computer">COMPUTER</option> 
				<option value = "physics">PHYSICS</option> 
				<option value = "chemistry">CHEMISTRY</option> 
				<option value = "hindi">HINDI</option> 
			</select> 
			<input type = "submit" name = "submit" value = Submit> 
		</form> 
	</body> 
</html> 

 

STEP 2: Fetch or print multiple selected value from list. In this case we are going to use form method and loop to fetch selected value in PHP

Code:

<html> 
	<body> 
		<!--name.php to be called on form submission--> 
		<form method = 'post'> 
			<h4>SELECT SUJECTS</h4> 
			
			<select name = 'subject[]' multiple size = 6> 
				<option value = 'english'>ENGLISH</option> 
				<option value = 'maths'>MATHS</option> 
				<option value = 'computer'>COMPUTER</option> 
				<option value = 'physics'>PHYSICS</option> 
				<option value = 'chemistry'>CHEMISTRY</option> 
				<option value = 'hindi'>HINDI</option> 
			</select> 
			<input type = 'submit' name = 'submit' value = Submit> 
		</form> 
	</body> 
</html> 
<?php 
	
	// Check if form is submitted successfully 
	if(isset($_POST["submit"])) 
	{ 
		// Check if any option is selected 
		if(isset($_POST["subject"])) 
		{ 
			// Retrieving each selected option 
			foreach ($_POST['subject'] as $subject) 
				print "You selected $subject<br/>"; 
		} 
	else
		echo "Select an option first !!"; 
	} 
?> 

 

posted May 19 in php by (10,020 points)   | 62 views
2,220 questions
2,691 answers
59 comments
241 users