multiple selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

multiple selection

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to take a look at the documentation for array_search() to understand how it works....
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Finally

Post 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>
Post Reply