Re: generating arrays using $_REQUEST/$_GET/$_POST
| From: | Eric Bolikowski | Date: | Thu, 22 Jan 2004 19:12:31 +0000 |
| Subject: | Re: generating arrays using $_REQUEST/$_GET/$_POST | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-175452@lists.php.net to get a copy of this message | ||
Hi Tristan
It seems wierd to me that $math would be an array even after that you have
named all your form fields to $math.
Well, what you can do otherwise that is a very known method is naming your
fields name[].
In your case this would be math[].
If you name many of your fields this way, $_GET['math'] will be an array
with auto-filled indexes.
Example:
---submit.html---
<form action="receiver.php" method="post">
Number 1: <input type="text" name="math[]"><br>
Number 2: <input type="text" name="math[]"><br>
Number 3: <input type="text" name="math[]"><br>
<input type="submit" name="submit" value="Submit">
</form>
-------
Now let's say you enter 1 in field 1, 2 in field 2 and 3 in field 3.
Now let's send the data to receiver.php
-------
---receiver.php---
<?php
$math = $_GET['math'];
foreach($key => $value as $math){
echo "$key => $value<br>";
?>
------
This would output:
1 => 1
2 => 2
3 => 3
-------
This method can also be very useful with selects, naming the select "name[]"
Then you when you submit, name[] will become an array where the selected
indexes are given in the array.
Hope you found this helpful
Eric
"Tristan Gutsche" <tristangutsche@optusnet.com.au> wrote in message
news:20040122182505.22215.qmail@pb1.pair.com...
> Hello,
>
> I am having problems generating an array based on data passed from a form.
> The form has a series of fields with the same name "math". When submited
if
> i have register_globals=on i have no problems calling a $math variable and
> it spits out all the values assigned to math, however with
> register_globals=off only the last value passed in the HTTP string is
being
> returned.
>
> Any suggestions welcome.
>
> Regards
> Tristan