hello...
i want ot select multiple vakues from dropdown and insert into database. but it is not working. any one plz help.
and one thing when ever i retrive values from database selected values come in top of the dropdown and remaing values are come in bottom of the dropdown.
for suppose i am selcting 1,4 from dropdown then when retriving that values from database that will come like this.

1
4
select
2
3
5

below is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div style="border:1px #FFFFFF ridge; width:70px; height:70px; overflow-x: auto; overflow-y: auto;">
<select name='color[]' style="border:0px;" size=6 multiple>
<option value='' selected>Select Item</option>
<option value='1'>Blue</option>
<option value='2'>Green</option>
<option value='3'>Red</option>
<option value='4'>Yellow</option>
<option value='5'>White</option>
</select>
</div>
</body>
</html>

I hope the following script solves your problem.

<?php
if(isset($_POST['clickhere'])) {
	$selected_values = $_POST['color'];
	foreach($selected_values as $key=>$value) {
                // Insert query comes here.
		echo "Insert Query - ".$value."<br />";
	}
	
	$select_blue = (in_array('1',$selected_values)) ? "selected" : "";
	$select_green = (in_array('2',$selected_values)) ? "selected" : "";
	$select_red = (in_array('3',$selected_values)) ? "selected" : "";
	$select_yellow = (in_array('4',$selected_values)) ? "selected" : "";
	$select_white = (in_array('5',$selected_values)) ? "selected" : "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
	<title>HTML Template</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta name="Generator" content="Dev-PHP 2.2.3" />
	<meta name="Keywords" content="your,keywords,here" />
	<meta name="Description" content="." />

	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />

	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<form name='testform' action='sometest.php' method='post'>
<select name='color[]' style="border:0px;" size=6 multiple>
<option value='' selected>Select Item</option>
<option value='1' <?php echo $select_blue;?> >Blue</option>
<option value='2'<?php echo $select_green;?> >Green</option>
<option value='3'<?php echo $select_red;?> >Red</option>
<option value='4'<?php echo $select_yellow;?> >Yellow</option>
<option value='5'<?php echo $select_white;?> >White</option>
</select>
<br /><br />
<input type='submit' name='clickhere' value='Click Here' />
</form>
</body>
</html>

I hope the following script solves your problem.

<?php
if(isset($_POST['clickhere'])) {
	$selected_values = $_POST['color'];
	foreach($selected_values as $key=>$value) {
                // Insert query comes here.
		echo "Insert Query - ".$value."<br />";
	}
	
	$select_blue = (in_array('1',$selected_values)) ? "selected" : "";
	$select_green = (in_array('2',$selected_values)) ? "selected" : "";
	$select_red = (in_array('3',$selected_values)) ? "selected" : "";
	$select_yellow = (in_array('4',$selected_values)) ? "selected" : "";
	$select_white = (in_array('5',$selected_values)) ? "selected" : "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
	<title>HTML Template</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta name="Generator" content="Dev-PHP 2.2.3" />
	<meta name="Keywords" content="your,keywords,here" />
	<meta name="Description" content="." />

	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />

	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<form name='testform' action='sometest.php' method='post'>
<select name='color[]' style="border:0px;" size=6 multiple>
<option value='' selected>Select Item</option>
<option value='1' <?php echo $select_blue;?> >Blue</option>
<option value='2'<?php echo $select_green;?> >Green</option>
<option value='3'<?php echo $select_red;?> >Red</option>
<option value='4'<?php echo $select_yellow;?> >Yellow</option>
<option value='5'<?php echo $select_white;?> >White</option>
</select>
<br /><br />
<input type='submit' name='clickhere' value='Click Here' />
</form>
</body>
</html>

thanks for your reply. i know this type. but i want to display selected values in top of the dropdown.
for example..........i am selecting 1,3 from "select, 1 , 2, 3, 4" .
i want to display in dropdown when i am retriving from database...

1
3
select
2
4

I hope the following script solves your problem.

<?php
if(isset($_POST['clickhere'])) {
	$selected_values = $_POST['color'];
	foreach($selected_values as $key=>$value) {
                // Insert query comes here.
		echo "Insert Query - ".$value."<br />";
	}
	
	$select_blue = (in_array('1',$selected_values)) ? "selected" : "";
	$select_green = (in_array('2',$selected_values)) ? "selected" : "";
	$select_red = (in_array('3',$selected_values)) ? "selected" : "";
	$select_yellow = (in_array('4',$selected_values)) ? "selected" : "";
	$select_white = (in_array('5',$selected_values)) ? "selected" : "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
	<title>HTML Template</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta name="Generator" content="Dev-PHP 2.2.3" />
	<meta name="Keywords" content="your,keywords,here" />
	<meta name="Description" content="." />

	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />

	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<form name='testform' action='sometest.php' method='post'>
<select name='color[]' style="border:0px;" size=6 multiple>
<option value='' selected>Select Item</option>
<option value='1' <?php echo $select_blue;?> >Blue</option>
<option value='2'<?php echo $select_green;?> >Green</option>
<option value='3'<?php echo $select_red;?> >Red</option>
<option value='4'<?php echo $select_yellow;?> >Yellow</option>
<option value='5'<?php echo $select_white;?> >White</option>
</select>
<br /><br />
<input type='submit' name='clickhere' value='Click Here' />
</form>
</body>
</html>

This is what i've been looking for weeks! But is there any way to convert this into wordpress style? Check out my topic about this issue please. Thanks.
http://www.daniweb.com/web-development/php/threads/370141

Member Avatar for diafol
$sel = (array) $_POST['color'];
$arr = range(1,5);
$top="";$bottom="";
foreach($arr as $val){
  $add = "<option value=\"$val\">$val</option>"; //you could add the selected attr here if req'd
  if(in_array($val,$sel)){
    $top.= $add;
  }else{
    $bottom.= $add; 
  }
}
echo $top . "<option disabled=\"disabled\">select</option>" . $bottom;
commented: @ardav. How smarty way. Vote up! :) Zero13 +7

I'm really a coding noob. Where this code should be added?

Member Avatar for diafol

Rereading your original post, I'm a little confused. perhaps this wasn't what you wanted after all. Sorry.

$list = array('a','b','c','d','e','f','g','h');

$ms = '<select size="32" name="abc" id="abc"   multiple="multiple" style="width: 276px; height:200px;" class="slct">';
		foreach($list as $sl){
$ms .='<option value='.$sl.'>'.$sl.'</option>';
			}
$sms .= '</select><br>

//to insert in db table
$multiselect = explode(",",$_POST['abc']);
	foreach($multiselect as $selected){
	mysql_query = ("insert into table_name (selected_let) values ('$selected')");
}

So where to add this code?

Where did you want to do that process ? You've not FORM already within your HTML, which means that how do you pass these drop-down value and process that ?
Perhaps, it may not what you want. But try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div>
<form name="colorswithcher" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color[]" size="6" multiple="multiple">
<?php 
$colors = array("BLUE", "GREEN", "RED", "YELLOW", "WHITE");
?>
<?php 
if(isset($_POST['changecolor'])){
$sel = (array) $_POST['color'];
$top="";
$bottom="";
$separator = '<option value="" disabled="disabled">Select</option>';
	foreach($colors as $value=>$label){
		if(in_array($value, $sel)) {
			$top .= '<option value="' . $value . '">' . $label . '</option>';
		}
		else{
			$bottom .= '<option value="' . $value . '">' . $label . '</option>';
		}
	}
	echo $top . $separator . $bottom;
}
else{
	foreach($colors as $value=>$label){
		echo '<option value="' . $value . '">' . $label . '</option>';
	}
}
?>
</select>
<br />
<input type="submit" value="Change Color" name="changecolor" />
</form>
</div>
</body>
</html>

@ardav already showed good method, and I used it inside it. If your problem solved, @ardav solved you properly than me.
Hope this help!

Member Avatar for diafol

OK ZERO13 - NO PROBS, threads are collaborative, not selfish. Using useful bits from different posts is what it's all about. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.