Home > Archive > PHP Language > January 2006 > $_POST
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]
|
|
|
| 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]
Is this right or do I have to use
$_POST['$lunch'] or
$_POST[lunch] or
$_POST['lunch']
Nothing I do seems to work.
The same seems to be wrong when I use the $_REQUEST[] function.
| |
|
| When you submit data from a form to a php script you can access that
submitted data using $_POST, or $_GET (depending how you submitted it)
or $_REQUEST. $_POST['lunch'] would be the correct way of doing it in
your case.
Completely aside from that if you want to remember that value so if a
person navigates to a different page it will retain that information
then you can use a session.
http://ie2.php.net/session
| |
| BearItAll 2005-12-13, 6:58 pm |
| On Mon, 12 Dec 2005 15:21:21 +0000, 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]
>
> Is this right or do I have to use
> $_POST['$lunch'] or
> $_POST[lunch] or
> $_POST['lunch']
> Nothing I do seems to work.
>
> The same seems to be wrong when I use the $_REQUEST[] function.
These aren't functions, they arrays. You could look at them like this,
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<input type='text' name='aname' />
<input type='text' name='age' />
<input type='submit' name='submit' />
<br><br><br>
<?php
if(isset($_POST['submit']))
{
foreach($_POST as $key => $value)
{
# $this->$key = $value;
echo "$key = $value<br>\n";
}
}
?>
</form>
If you enter "hello" and "45" it will output
aname = hello
age = 56
submit =
| |
|
| On 12 Dec 2005 07:35:10 -0800, "Sean" <oreilly.sean@gmail.com> wrote:
>When you submit data from a form to a php script you can access that
>submitted data using $_POST, or $_GET (depending how you submitted it)
>or $_REQUEST. $_POST['lunch'] would be the correct way of doing it in
>your case.
>
>Completely aside from that if you want to remember that value so if a
>person navigates to a different page it will retain that information
>then you can use a session.
>
>http://ie2.php.net/session
Thank you for your quickness. Unfortunatly I cannt make it work. should I
go $menu=$_POST['$variable']
I have every page with session_start(); at the top in it.
| |
| Hilarion 2005-12-14, 7:55 am |
| 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
| |
| Kimmo Laine 2005-12-14, 7:55 am |
| "SG" <steffi_grant45@hotmail.com> wrote in message
news:1s00q15a2854smsq1r8dkehkgdgdc59735@
4ax.com...
> On 12 Dec 2005 07:35:10 -0800, "Sean" <oreilly.sean@gmail.com> wrote:
>
> Thank you for your quickness. Unfortunatly I cannt make it work. should I
> go $menu=$_POST['$variable']
> I have every page with session_start(); at the top in it.
Okay, the syntax is $_POST['variable'] (notice there isn't a dollar sign in
'variable') and it has nothing to do with sessions. If you want to store a
posted variable to session, that's something you need to separately. In that
case you'd put
$_SESSION['variable'] = $_POST['variable'] on the page that handles the form
(ie. the action="" page that you set in the form).
Really simple 1-2-3 example:
page1.html
<form method="post" action="page2.php">
<input name="variable"><input type="submit">
</form>
page2.php:
<?php
session_start();
$_SESSION['variable'] = $_POST['variable'];
echo $_POST['variable'] // This should now print what you posted on page1
?>
page3.php:
<?php
session_start();
echo $_SESSION['variable']; // This should now print what you posted on
page1
?>
--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
antaatulla.sikanautaa@gmail.com.NOSPAM.invalid
| |
|
| On Wed, 14 Dec 2005 13:00:27 +0100, "Hilarion" <hilarion@SPAM.op.SMIECI.pl>
wrote:
>SG wrote:
>
>
>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').
>
>
>
>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).
>
>
>
>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).
>
>
>
>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).
>
>
>
>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
Thank you evrybody for all your nice responses and with your help I make it
work.
| |
| Jim Michaels 2006-01-15, 9:55 pm |
| $_POST[lunch] is used inside strings.
echo "eat some $_POST[lunch] at 4:00pm.<br>";
| |
| David Haynes 2006-01-15, 9:55 pm |
| Jim Michaels wrote:
> $_POST[lunch] is used inside strings.
>
>
> echo "eat some $_POST[lunch] at 4:00pm.<br>";
>
>
Shouldn't that be
echo "eat some $_POST['lunch'] at 4:00pm.<br>":
-david-
| |
| Hilarion 2006-01-16, 9:55 pm |
| >> $_POST[lunch] is used inside strings.
> Shouldn't that be
> echo "eat some $_POST['lunch'] at 4:00pm.<br>":
It should be (note the curly braces):
echo "eat some {$_POST['lunch']} at 4:00pm.<br>";
I'm not sure how it works when we have "lunch"
constant defined for example to value "sth".
I'd avoid such situations so noone would have
to make sure if I wanted to use this defined
constant as the array index (but made a typo
and entered the quotes), or if I wanted to
use string "lunch" as this index or something
like that.
I prefer this syntax:
echo 'eat some ' . $_POST['lunch'] . ' at 4:00pm.<br>;
or this one:
printf( 'eat some %s at 4:00pm.<br>', $POST['lunch'] );
Both use single quotes to turn off the string parsing
and make the code a bit easier to interprete (also
when I want to use "$" sign or double quotes in the
string, but not when I want to use "\n" escape sequences).
Hilarion
| |
| Jim Michaels 2006-01-17, 3:55 am |
| actually it works fine without the quotes. I don't remember, but I think
you get an error if you use quotes there.
"David Haynes" <david.haynes2@sympatico.ca> wrote in message
news:j8Byf.7830$x8.5938@fe04.usenetserver.com...
> Jim Michaels wrote:
> Shouldn't that be
> echo "eat some $_POST['lunch'] at 4:00pm.<br>":
>
> -david-
>
|
|
|
|
|