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
ademus
Forum Newbie
Posts: 10
Joined: Thu Feb 16, 2006 7:00 am

multiple selection

Post by ademus »

Hi,
I have problem when I try to keep the selected items still selected after posting the page.
I've found a thread in which this is subject is solved but the solution provided won't work for me.
The code is:

Code: Select all

<select name="provdist[]" class="form-select-mult" id="provdist" size="4" multiple >
		<?php	
			$provincias = mssql_query('select * from md_provincias', $db->dblink);
			while ( $row = mssql_fetch_row($provincias) ){
				if ( array_search($row[0], $_POST['provdist'] ) ) {
					echo('<option value=' . '\'' . $row[0] . '\'' . ' selected >' . $row[1] . '</option>');
					continue;
				}
				echo('<option value=' . '\'' . $row[0] . '\'' . '>' . $row[1] . '</option>');
			}
		?>								
	</select>
And the error is:

Code: Select all

<b>Warning</b>:  array_search() [<a href='function.array-search'>function.array-search</a>]: Wrong datatype for second argument in <b>I:\WebServer\DOCEK\suplementos.php</b> on line <b>239</b><br />
Is there any mistake?
Thanks in advance !!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST['provdist'] isn't an array. I'd guess it doesn't exist when this happens.
ademus
Forum Newbie
Posts: 10
Joined: Thu Feb 16, 2006 7:00 am

Post by ademus »

$_POST['provdist'] isn't an array. I'd guess it doesn't exist when this happens.
but provdist is the name of the select defined four lines befor the array_search call... And is defined as

Code: Select all

<select name="provdist[]" class="form-select-mult" id="provdist" size="4" multiple >
so i guess that it should be recognized as an array, shouldn't it ??
thanks feyd !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it doesn't magically appear just because you're echoing a form tag until after that form it submitted to that same script.
gavinandresen
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 8:18 pm

Post by gavinandresen »

My fillInFormValues() function Does the Right Thing with multiple <select>. See: http://www.onlamp.com/pub/a/php/2006/03 ... forms.html
ademus
Forum Newbie
Posts: 10
Joined: Thu Feb 16, 2006 7:00 am

Post by ademus »

My fillInFormValues() function Does the Right Thing with multiple <select>. See: http://www.onlamp.com/pub/a/php/2006/03 ... forms.html
Sorry gavin, but i can't see where in your fillInFormValues() function you work with multiple <select>
gavinandresen
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 8:18 pm

Post by gavinandresen »

ademus wrote: Sorry gavin, but i can't see where in your fillInFormValues() function you work with multiple <select>
I modified the long example code to add your multi-valued select:
http://www.skypaint.com/gavin/code/longExample.php

... and I updated the code in the .zip file.
gavinandresen
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 8:18 pm

Post by gavinandresen »

Oh, and you're getting an array_search() error probably because initially $_POST['provdist'] isn't set. If you don't wanna use fillInFormValues(), doing something like this at the top of your php code:

Code: Select all

if (!isset($_POST['provdist'])) { $_POST['provdist'] = array(); }
... should fix the problem.
Post Reply