Connecting Tech Pros Worldwide Forums | Help | Site Map

array in $_GET ...

dude
Guest
 
Posts: n/a
#1: Jun 16 '06
i'll try to be short ... i have this in html :

<select name="OS[]" size="5">
<option value="0" selected>Please select one or more...</option>
<option value="1">Windows</option>
<option value="2">Mac OS X</option>
<option value="3">Linux</option>
</select>

and i want to echo the values from this simple list (OS), i mean array
from $_GET array ...

i did this:

foreach($_GET as $i => $val)
echo $i . ": " . $val . "<br>";

but, it only echoes out OS: Array ...

and this: echo $_GET['OS'][1]."<br>\n"; but it is wrong ...

how to extract values from OS array inside te GET ? please help ...

Erwin Moller
Guest
 
Posts: n/a
#2: Jun 16 '06

re: array in $_GET ...


dude wrote:
[color=blue]
> i'll try to be short ... i have this in html :
>
> <select name="OS[]" size="5">
> <option value="0" selected>Please select one or more...</option>
> <option value="1">Windows</option>
> <option value="2">Mac OS X</option>
> <option value="3">Linux</option>
> </select>
>
> and i want to echo the values from this simple list (OS), i mean array
> from $_GET array ...
>
> i did this:
>
> foreach($_GET as $i => $val)
> echo $i . ": " . $val . "<br>";
>
> but, it only echoes out OS: Array ...
>
> and this: echo $_GET['OS'][1]."<br>\n"; but it is wrong ...
>[/color]
[color=blue]
> how to extract values from OS array inside te GET ? please help ...[/color]

Hi,

Your select is not an array, but a simple value, since only 1 can be
selected.
So if you want to know what was selected, just use:
<select name="OS" size="5">

and from PHP: $_GET["OS"]

Of course this means the method of the form was GET and not POST, in which
case you should use $_POST["OS"]

I think you are confusing checkboxes with select.
For checkboxes you can use:
<input type="checkbox" name="myval[]" value="1">1<br>
<input type="checkbox" name="myval[]" value="2">2<br>
<input type="checkbox" name="myval[]" value="3">3<br>

then from PHP:
$myvalArr = $_POST["myval"];

Regards,
Erwin Moller


Kimmo Laine
Guest
 
Posts: n/a
#3: Jun 16 '06

re: array in $_GET ...


"dude" <dude.j@gmail.com> wrote in message
news:e6u5nn$3g3$1@ss408.t-com.hr...[color=blue]
> i'll try to be short ... i have this in html :
>
> <select name="OS[]" size="5">
> <option value="0" selected>Please select one or more...</option>
> <option value="1">Windows</option>
> <option value="2">Mac OS X</option>
> <option value="3">Linux</option>
> </select>
>
> and i want to echo the values from this simple list (OS), i mean array
> from $_GET array ...
>
> i did this:
>
> foreach($_GET as $i => $val)
> echo $i . ": " . $val . "<br>";[/color]


try:
foreach($_GET['OS'] as $i => $val)
echo $i . ": " . $val . "<br>";

By the way, I think you're missing the attribute MULTIPLE from the select if
you want to be able to select multiple items. It should be
<select name="OS[]" size="5" multiple>

or if you're using xhtml,
<select name="OS[]" size="5" multiple="multiple">



--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


dude
Guest
 
Posts: n/a
#4: Jun 16 '06

re: array in $_GET ...


Erwin Moller wrote:[color=blue]
> dude wrote:
>
>[color=green]
>>i'll try to be short ... i have this in html :
>>
>><select name="OS[]" size="5">
>> <option value="0" selected>Please select one or more...</option>
>> <option value="1">Windows</option>
>> <option value="2">Mac OS X</option>
>> <option value="3">Linux</option>
>></select>
>>
>>and i want to echo the values from this simple list (OS), i mean array
>>from $_GET array ...
>>
>>i did this:
>>
>>foreach($_GET as $i => $val)
>> echo $i . ": " . $val . "<br>";
>>
>>but, it only echoes out OS: Array ...
>>
>>and this: echo $_GET['OS'][1]."<br>\n"; but it is wrong ...
>>[/color]
>
>[color=green]
>>how to extract values from OS array inside te GET ? please help ...[/color]
>
>
> Hi,
>
> Your select is not an array, but a simple value, since only 1 can be
> selected.
> So if you want to know what was selected, just use:
> <select name="OS" size="5">
>
> and from PHP: $_GET["OS"]
>
> Of course this means the method of the form was GET and not POST, in which
> case you should use $_POST["OS"]
>
> I think you are confusing checkboxes with select.
> For checkboxes you can use:
> <input type="checkbox" name="myval[]" value="1">1<br>
> <input type="checkbox" name="myval[]" value="2">2<br>
> <input type="checkbox" name="myval[]" value="3">3<br>
>
> then from PHP:
> $myvalArr = $_POST["myval"];
>
> Regards,
> Erwin Moller[/color]

thnx, but it is not about checkboxes ... this works fine with checkboxes
... not with list :)
dude
Guest
 
Posts: n/a
#5: Jun 16 '06

re: array in $_GET ...


> try:[color=blue]
> foreach($_GET['OS'] as $i => $val)
> echo $i . ": " . $val . "<br>";
>
> By the way, I think you're missing the attribute MULTIPLE from the select if
> you want to be able to select multiple items. It should be
> <select name="OS[]" size="5" multiple>
>
> or if you're using xhtml,
> <select name="OS[]" size="5" multiple="multiple">[/color]

i did set multiple, and the foreach and the echo result is:
if all the items in the list are selected :
0: 1
1: 2
2: 3
3: 3

if second selected then the echo will be: 0: 1

ok, i can work this way, but can it echo values i mean selected string,
like Windows, Linux, ... , not 0:1, 0:2, etc ...
is it possible ?
eccept if ... or switch condition

foreach($_GET['OS'] as $i => $val)
if($i == 0 && $val == 1)
echo "Windows <br>";
if($i == 0 && $val == 2)
echo "Linux <br>";
... etc

this is too complicated, what if i have 40 items in the list :)




David Haynes
Guest
 
Posts: n/a
#6: Jun 16 '06

re: array in $_GET ...


dude wrote:[color=blue][color=green]
>> try:
>> foreach($_GET['OS'] as $i => $val)
>> echo $i . ": " . $val . "<br>";
>>
>> By the way, I think you're missing the attribute MULTIPLE from the
>> select if you want to be able to select multiple items. It should be
>> <select name="OS[]" size="5" multiple>
>>
>> or if you're using xhtml,
>> <select name="OS[]" size="5" multiple="multiple">[/color]
>
> i did set multiple, and the foreach and the echo result is:
> if all the items in the list are selected :
> 0: 1
> 1: 2
> 2: 3
> 3: 3
>
> if second selected then the echo will be: 0: 1
>
> ok, i can work this way, but can it echo values i mean selected string,
> like Windows, Linux, ... , not 0:1, 0:2, etc ...
> is it possible ?
> eccept if ... or switch condition
>
> foreach($_GET['OS'] as $i => $val)
> if($i == 0 && $val == 1)
> echo "Windows <br>";
> if($i == 0 && $val == 2)
> echo "Linux <br>";
> ... etc
>
> this is too complicated, what if i have 40 items in the list :)
>
>
>
>[/color]

Why are you using 1, 2, 3, etc. when you want Windows, Linux, etc.?

Perhaps this little example will help:
<?php
if( isset($_GET['OS']) ) {
foreach( $_GET['OS'] as $key => $value) {
printf("Selected %s<br>\n", $value);
}
}
?>
<html>
<head></head>
<body>
<form action="test.php" method="GET">
<select name="OS[]" size="5" multiple>
<option>Windows</option>
<option>Linux</option>
</select>
<input type="submit">
</form>
</body>
</html>

-david-


ED
Guest
 
Posts: n/a
#7: Jun 16 '06

re: array in $_GET ...



"dude" <dude.j@gmail.com> wrote in message
news:e6u8g5$aau$1@ss408.t-com.hr...[color=blue][color=green]
>> try:
>> foreach($_GET['OS'] as $i => $val)
>> echo $i . ": " . $val . "<br>";
>>
>> By the way, I think you're missing the attribute MULTIPLE from the select
>> if you want to be able to select multiple items. It should be
>> <select name="OS[]" size="5" multiple>
>>
>> or if you're using xhtml,
>> <select name="OS[]" size="5" multiple="multiple">[/color]
>
> i did set multiple, and the foreach and the echo result is:
> if all the items in the list are selected :
> 0: 1
> 1: 2
> 2: 3
> 3: 3
>
> if second selected then the echo will be: 0: 1
>
> ok, i can work this way, but can it echo values i mean selected string,
> like Windows, Linux, ... , not 0:1, 0:2, etc ...
> is it possible ?
> eccept if ... or switch condition
>
> foreach($_GET['OS'] as $i => $val)
> if($i == 0 && $val == 1)
> echo "Windows <br>";
> if($i == 0 && $val == 2)
> echo "Linux <br>";
> ... etc
>
> this is too complicated, what if i have 40 items in the list :)
>
>
>
>[/color]

Looks like you're overcomplicating things.
Just change the options to pass the string instead of an index (unless you
need the indes for something else):

<select name="OS[]" size="5" multiple="multiple">
<option value="" selected>Please select one or more...</option>
<option value="Windows">Windows</option>
<option value="Mac OS X">Mac OS X</option>
<option value="Linux">Linux</option>
</select>

Then
$myarray = $_GET['OS'];
will then give you an array of the selected Strings

cheers,
ED


dude
Guest
 
Posts: n/a
#8: Jun 16 '06

re: array in $_GET ...


Thank you all ! I'm grateful. This works perfect!

Looks like I was overcomplicating things :)
Erwin Moller
Guest
 
Posts: n/a
#9: Jun 19 '06

re: array in $_GET ...


dude wrote:
[color=blue]
> Erwin Moller wrote:[color=green]
>> dude wrote:
>>
>>[color=darkred]
>>>i'll try to be short ... i have this in html :
>>>
>>><select name="OS[]" size="5">
>>> <option value="0" selected>Please select one or more...</option>
>>> <option value="1">Windows</option>
>>> <option value="2">Mac OS X</option>
>>> <option value="3">Linux</option>
>>></select>
>>>
>>>and i want to echo the values from this simple list (OS), i mean array
>>>from $_GET array ...
>>>
>>>i did this:
>>>
>>>foreach($_GET as $i => $val)
>>> echo $i . ": " . $val . "<br>";
>>>
>>>but, it only echoes out OS: Array ...
>>>
>>>and this: echo $_GET['OS'][1]."<br>\n"; but it is wrong ...
>>>[/color]
>>
>>[color=darkred]
>>>how to extract values from OS array inside te GET ? please help ...[/color]
>>
>>
>> Hi,
>>
>> Your select is not an array, but a simple value, since only 1 can be
>> selected.
>> So if you want to know what was selected, just use:
>> <select name="OS" size="5">
>>
>> and from PHP: $_GET["OS"]
>>
>> Of course this means the method of the form was GET and not POST, in
>> which case you should use $_POST["OS"]
>>
>> I think you are confusing checkboxes with select.
>> For checkboxes you can use:
>> <input type="checkbox" name="myval[]" value="1">1<br>
>> <input type="checkbox" name="myval[]" value="2">2<br>
>> <input type="checkbox" name="myval[]" value="3">3<br>
>>
>> then from PHP:
>> $myvalArr = $_POST["myval"];
>>
>> Regards,
>> Erwin Moller[/color]
>
> thnx, but it is not about checkboxes ... this works fine with checkboxes
> ... not with list :)[/color]

Yes it does.
DId you read my first piece of code?

<select name="OS" size="5">
and from PHP: $_GET["OS"]

What is excactly not working?

Regards,
Erwin Moller



Closed Thread