Page 1 of 1

$_POST[] issue for select multiple

Posted: Wed Mar 07, 2018 5:12 pm
by delorme
Hi from France
My livecode server code is:

<!DOCTYPE html><html><?lc
put "hello" & $_POST["cars"]
?></html>

When I post from a multiple select input with several options selected in the following html page:

<!DOCTYPE html><html><body>
<form method="post" action="mylivecodeserver">
<select name="cars" multiple >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
<input type="submit">
</form></body></html>

I only get (by $_post) the LAST selected option value.
I tried name="cars[]" for getting an array as cited in some php forums, but a "is not an array" empty value is returned.
What is wrong with not getting ALL selected values?

Thanks for your kind help
Regards from Jean-Marc

Re: $_POST[] issue for select multiple

Posted: Wed Mar 07, 2018 7:35 pm
by FourthWorld
delorme wrote:
Wed Mar 07, 2018 5:12 pm
I tried name="cars[]" for getting an array as cited in some php forums, but a "is not an array" empty value is returned.
What is wrong with not getting ALL selected values?
Without seeing the code you're using to get the array keys it will not be possible to provide ways to improve it.

If you're using "cars[]", that may be the issue; try simply "cars" instead. But that's only a guess, since I haven't seen the actual code that's failing.

Re: $_POST[] issue for select multiple

Posted: Wed Mar 07, 2018 10:44 pm
by bangkok

Code: Select all

<!DOCTYPE html><html><?lc
put "Il y a "&the number of elements of $_POST["cars"]&" reponses<BR>"
repeat for each element tItem in $_POST["cars"]
put tItem&"<BR>"
end repeat
?></html>

Et voilà !
:)

Et il faut mettre :

<select name="cars[]" multiple >

Last but not least :

The array is passed as $_POST["cars"][1] (for the first select)
then
$_POST["cars"][2]
etc.

So you could do as well :

Code: Select all

<!DOCTYPE html><html><?lc
put the number of elements of $_POST["cars"] into tNumber
put "Il y a "&tNumber&" reponses<BR>"
repeat with i = 1 to tNumber
put  $_POST["cars"][i]&"<BR>"
end repeat
?></html>