Page 1 of 1

multiple selection

Posted: Sat Feb 11, 2006 7:36 pm
by AYYASH
Hi,

I have problem when I want to keep the selected items still selected after posting the page in $_POST[PHP_SELF] form.
It is easy to do it in other form fields but not the multiple selection field. I tried to do it in many ways but it won't work.

after I connect to DB and select the DB, here is the way I tired to do it:

Code: Select all

<select multiple name="addtool_nmae">
<?php 
 while($row = mysql_fetch_array($query)){
 		$row = $row['addtool_name'];
 		
 		if ($row = $_POST['addtool_name']) {
 			$selected = " selected";
 		}
 	print "<option value=\"$row\"$selected>$row</option>";
 } // while

?>
</select>

Posted: Sat Feb 11, 2006 8:06 pm
by feyd
your if has a logic error. You are assigning $_POST['addtool_name'] to $row. == is what you should be using. Additionally, your select has a different name.

But that won't fix your problem. If you named your select "addtool_name[]" then used array_search() to see if $row['addtool_name'] as in $_POST['addtool_name'] it would start working.

Posted: Sat Feb 11, 2006 11:31 pm
by AYYASH
Thanks for your reply,

I've changed the code to this but still not working.

Code: Select all

<select name="addtool_name[]" size="10" multiple class="cel">
	 <?php while ($row_add = mysql_fetch_array($query03)) {
	 	 $value = $row_add['addtool_name'];
		 if (array_search($value == $_POST['addtool_name'])) {
		 	$selected = " selected"; 
			} else {
				$selected = "";
			}
		  print "<option value=\"$value\"$selected>$value</option>";
		 }
	 ?>    
	 </select>

Posted: Sat Feb 11, 2006 11:40 pm
by feyd
you may want to take a look at the documentation for array_search() to understand how it works....

Finally

Posted: Sun Feb 12, 2006 12:49 am
by AYYASH
Thanks

It finally worked.

Code: Select all

<select name="addtool_name[]" size="10" multiple class="cel">
	 <?php while ($row_add = mysql_fetch_array($query03)) {
	 	 $value = $row_add['addtool_name'];
		 if (FALSE !== array_search($value, $_POST['addtool_name'])) {
		 	$selected = " selected"; 
			} else {
				$selected = "";
			}
		  print "<option value=\"$value\"$selected>$value</option>";
		 }
	 ?>    
	 </select>