Page 1 of 1

multiple selection

Posted: Fri Mar 24, 2006 6:55 pm
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 !!

Posted: Fri Mar 24, 2006 6:57 pm
by feyd
$_POST['provdist'] isn't an array. I'd guess it doesn't exist when this happens.

Posted: Fri Mar 24, 2006 7:17 pm
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 !

Posted: Fri Mar 24, 2006 7:38 pm
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.

Posted: Sat Mar 25, 2006 7:29 am
by gavinandresen
My fillInFormValues() function Does the Right Thing with multiple <select>. See: http://www.onlamp.com/pub/a/php/2006/03 ... forms.html

Posted: Sat Mar 25, 2006 2:20 pm
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>

Posted: Sun Mar 26, 2006 7:17 pm
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.

Posted: Sun Mar 26, 2006 7:20 pm
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.