|
Posted by Hilarion on 09/29/27 11:34
SG wrote:
> Please bear with me as i'm new t ophp. In page 1 I ask for a variable in a
> form, like <input type=text name="lunch"> and the next file handes the
> variable.
>
> If I want to use the same variable in page3 or page 103, I think I have to
> use
> $_POST[$lunch]
Wrong (unless you want to acces some field of this array based on the
$lunch variable value, eg. when $lunch is set to 'a', then $_POST[$lunch]
would refer to $_POST['a'], but it's not the case here because you
wanted to access the value passed from <input> called 'lunch').
>
> Is this right or do I have to use
> $_POST['$lunch'] or
Also wrong (unless you really want to access field named '$lunch' - with
the dollar sign, but this is not the case because your <input> field
is called 'lunch' - without the dollar sign).
> $_POST[lunch] or
This will work sometimes but it's also wrong. In this case PHP treats
word "lunch" as a defined constant. If you'd define it somewhere before,
then this word would be replaced by the value of the defined constant.
If you did not define it before, then PHP will replace this reference
to a non-defined constant with it's name, which will make it work.
It means that if you want to use defined constant, then this solution
is OK, but if you want to simply access field named "lunch", then it's
not a correct way (see next one).
> $_POST['lunch']
This one is OK. You could also use $_POST["lunch"] but I personaly
think that in this case the single quote is better (it gets parsed
a tiny bit faster).
> Nothing I do seems to work.
>
> The same seems to be wrong when I use the $_REQUEST[] function.
Those are not functions but arrays.
The main problem is that when first page has a form which sends
data to second page, then to have those values available in third
page you'd have to pass them somehow to it - they do NOT automagically
stay in $_POST, $_GET or $_REQUEST. You can do it by placing the
values in $_SESSION in the second page code (from $_POST or $_REQUEST)
and read them from $_SESSION in the third page. You'd have to configure
your server to handle sessions properly first. You can also do it
by creating a <form> on the second page with hidden fields containing
all the values passed from the first page and make submiting the
<form> the only way to get to the third page. This way you'll get
all the values in $_POST or $_GET (depending on the method specified
for the <form>) and in $_REQUEST and you'd not have to use (and
configure) sessions and this way will not require user to allow
cookies (when the session is configured to pass it's ID or values
in cookies) and will not require you to explicitly pass session ID
in URL (when the session is configured not to use cookies or when
the user does not allow cookies).
Hilarion
Navigation:
[Reply to this message]
|