Home > Archive > PHP Programming > August 2006 > newbie question: using $_POST inside of array parameters?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
newbie question: using $_POST inside of array parameters?
|
|
| kurrent@gmail.com 2006-08-29, 6:58 pm |
| I am relatively new to PHP and just have a question regarding $_POST to
get a value passed via a form to be included of the parameter of an
array I am using:
eg.
<?
$a array = ("1", "2", "3");
echo "<form method=POST>
<select name=b size=1>
<option value=1>number1</option>
<option value=2>number2</option>
<option value=0>number3</option>
</select>
<input type=submit name=Submit>
</form>";
echo "$a[$b]";
Since I have a global variables turned off, and would like to keep
learning with them off, I am stumped on to get that value passed
>From my understanding, I see that $_POST[b] would successfully retrive
the value of the $b variable. So i've tried many variants on the syntax
but no luck (e.g. "$a[$_POST[b]]) etc...
any help is much appreciated :) thanks!
| |
| Alvaro G. Vicario 2006-08-29, 6:58 pm |
| *** kurrent@gmail.com escribió/wrote (29 Aug 2006 14:24:02 -0700):
> <select name=b size=1>
> <option value=1>number1</option>
> <option value=2>number2</option>
> <option value=0>number3</option>
> </select>
> the value of the $b variable. So i've tried many variants on the syntax
> but no luck (e.g. "$a[$_POST[b]]) etc...
I'm unsure of what you're trying to accomplish, but you must learn the
syntax of the different types of identifiers:
$foo -> variable
foo() -> function or class
'foo' -> string
"foo" -> string with variables
foo -> constant
So $_POST[b] won't make any sense unless you previously define a constant
called b:
define('b', 'whatever');
I suppose you just forgot the quotes.
Your sample form creates the following array:
$_POST['b']='1';
$_POST['Submit']='Enviar consulta';
You can see it yourself with var_dump($_POST).
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
| |
| onembk 2006-08-29, 6:58 pm |
| On 2006-08-29 15:24:02 -0600, kurrent@gmail.com said:
> I am relatively new to PHP and just have a question regarding $_POST to
> get a value passed via a form to be included of the parameter of an
> array I am using:
>
> eg.
>
> <?
>
> $a array = ("1", "2", "3");
>
> echo "<form method=POST>
>
> <select name=b size=1>
> <option value=1>number1</option>
> <option value=2>number2</option>
> <option value=0>number3</option>
> </select>
>
> <input type=submit name=Submit>
>
>
> </form>";
>
> echo "$a[$b]";
>
>
> Since I have a global variables turned off, and would like to keep
> learning with them off, I am stumped on to get that value passed
>
>
> the value of the $b variable. So i've tried many variants on the syntax
> but no luck (e.g. "$a[$_POST[b]]) etc...
>
> any help is much appreciated :) thanks!
You need to either
echo $a[$_POST['b']];
or
$b = $_POST['b'];
echo $a[$b];
(the b is in quotes).
| |
| Norman Peelman 2006-08-29, 9:57 pm |
| <kurrent@gmail.com> wrote in message
news:1156886642.624061.318070@m73g2000cwd.googlegroups.com...
> I am relatively new to PHP and just have a question regarding $_POST to
> get a value passed via a form to be included of the parameter of an
> array I am using:
>
> eg.
>
> <?
>
> $a array = ("1", "2", "3");
>
> echo "<form method=POST>
>
> <select name=b size=1>
> <option value=1>number1</option>
> <option value=2>number2</option>
> <option value=0>number3</option>
> </select>
>
> <input type=submit name=Submit>
>
>
> </form>";
>
> echo "$a[$b]";
>
>
> Since I have a global variables turned off, and would like to keep
> learning with them off, I am stumped on to get that value passed
>
>
> the value of the $b variable. So i've tried many variants on the syntax
> but no luck (e.g. "$a[$_POST[b]]) etc...
>
> any help is much appreciated :) thanks!
>
How about : echo "{$a[$_POST['b']]}";
Norm
| |
| Kimmo Laine 2006-08-30, 7:57 am |
| "Alvaro G. Vicario" <webmaster@NOSPAMdemogracia.com> wrote in message
news:rj72wmibg02j.622tzkodwvj7.dlg@40tude.net...
> *** kurrent@gmail.com escribió/wrote (29 Aug 2006 14:24:02 -0700):
>
>
>
> I'm unsure of what you're trying to accomplish, but you must learn the
> syntax of the different types of identifiers:
>
> $foo -> variable
> foo() -> function or class
> 'foo' -> string
> "foo" -> string with variables
> foo -> constant
>
> So $_POST[b] won't make any sense unless you previously define a constant
> called b:
>
> define('b', 'whatever');
That's not entirely true. if there is no constant named foo, it is
interpreted as "bare string", an unquoted string. Ie. it is treated just
like a string, but a warnign message is generated. So basicly $a[$_POST[b]]
would work. This is a bad practise and should be avoided always to
distinguish the difference between a constant and a string. Bare strings is
a terrible terrible feature implemented only for backwards compatibility, I
can't imagine anyone seriously using it. But it's there, and it works. A
programmer should be aware of the peculiar features of the language even
thou they aren't used.
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg)
| |
| mootmail-googlegroups@yahoo.com 2006-08-30, 7:57 am |
| Norman Peelman wrote:
> <kurrent@gmail.com> wrote in message
> news:1156886642.624061.318070@m73g2000cwd.googlegroups.com...
>
> How about : echo "{$a[$_POST['b']]}";
>
> Norm
Building upon that advice, there is a limitation when using
double-quoted strings in PHP which you should be aware of.
Double-quoted strings will be parsed for variables, however only of the
simplest types. Anything more complex needs to be encased in curly
braces { } in order to be evaluated.
So while you can do:
echo "$foo"; \\evaluates variable $foo
You cannot do:
echo "$foo['a']";
echo "$foo[$x]";
echo "$foo->x";
Instead, you must do:
echo "{$foo['a']}";
echo "{$foo[$x]}";
echo "{$foo->x}";
| |
| kurrent@gmail.com 2006-08-31, 6:57 pm |
| It worked, thanks alot for the feedback guys :)
much appreciated
|
|
|
|
|