Re: SELECT tags in PHP, addressing selected options
| From: | Matthew Leingang | Date: | Fri, 29 Sep 2000 14:20:36 +0000 |
| Subject: | Re: SELECT tags in PHP, addressing selected options | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18050@lists.php.net to get a copy of this message | ||
Hello Liam,
Remember how a browser submits form values from SELECT boxes.
If you have in a form
<select name="list" mutiple>
<option value=1>Choice 1</option>
<option value=2>Choice 2</option>
<option value=3>Choice 3</option>
<option value=4>Choice 4</option>
</select>
And the user selects, say, 1, 2, and 4 and submits, the browser (which
doesn't have any idea what kind of script will be receiving this query)
makes the call to
http://www.yoursite.com/yourscript.php?list=1&list=2&list=4
Once PHP starts processing, it reads the query string and does the effect
of the following code:
$list = 1;
$list = 2;
$list = 4;
So the script the form is submitted to will have a SCALAR variable with
the same name as the form element with value equal to the last option
selected.
OK, all of that to explain why this works like it should: Suppose instead
the form name is "list[]". Then the browser sends a query which is a
urlencode()d form of:
...?list[]=1&list[]=2&list[]=4
And PHP will process something that resembles:
$list[] = 1;
$list[] = 2;
$list[] = 4;
And the result is an ARRAY with entries for each option selected.
Beautiful, ain't it? If you don't think so, try writing a perl script
that does the same thing from scratch! Not trivial if you're new to
programming.
--Matt
----------------------------------------------------------------
Matthew Leingang http://www.math.rutgers.edu/
Rutgers University leingang@math.rutgers.edu
Department of Mathematics "This signature needs no quote."
On Thu, 28 Sep 2000, Liam Gibbs wrote:
> I'm having a problem with PHP/HTML. My problem is this:
>
> I have a <SELECT NAME = "list" MULTIPLE> list box in an HTML form. In
> that, obviously, users can select multiple choices. They click the
> submit button. The action for the form is to submit it to a PHP page
> which will list all of the selected options, which are
> <OPTION>Choice1</OPTION>.
>
> But how? I can't figure out how to address those selected options.
> Would it be $Choice1? Should I include a NAME attribute? Does anyone
> know how to address these options?
>
> Liam Gibbs
>
> [ Part 37: "Included Message" ]
>