For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > $_POST problems









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 $_POST problems
Isaac Hunt

2006-01-10, 4:00 am

Hi

I'm trying to get variables POSTed from a form. What I'm looking for is a
way of getting all variables in one shot. The form contains (amongst other
things) the following lines:-

<input type="hidden" name="action" value="checknew">
<input size="50" name="emai1" value="<? echo $email; ?>">

I'm trying to read them back in using:-

foreach($_POST as $key => $value)
{
# $this->$key = $value;
echo "$key = $value<br>\n";
}

This is showing all the vaiables without a problem but if I put the
following straight after I get nothing

echo " ACTION = ".$action;

Adding a $action=$_POST[action];
works fine.
Is there a simple way to get all the variables without doing a $_POST[] for
each one? That's a bit long winded if there's 80 plus to read in.
register_globals is off.


--
Isaac Hunt
Possibly the most handsome man here?
Carl

2006-01-10, 4:00 am

Isaac Hunt wrote:
> Hi
>
> I'm trying to get variables POSTed from a form. What I'm looking for is a
> way of getting all variables in one shot. The form contains (amongst other
> things) the following lines:-
>
> <input type="hidden" name="action" value="checknew">
> <input size="50" name="emai1" value="<? echo $email; ?>">
>
> I'm trying to read them back in using:-
>
> foreach($_POST as $key => $value)
> {
> # $this->$key = $value;
> echo "$key = $value<br>\n";
> }
>
> This is showing all the vaiables without a problem but if I put the
> following straight after I get nothing
>
> echo " ACTION = ".$action;
>
> Adding a $action=$_POST[action];
> works fine.
> Is there a simple way to get all the variables without doing a $_POST[] for
> each one? That's a bit long winded if there's 80 plus to read in.
> register_globals is off.
>
>


Isaac,

Yes, but it it not recommended. If you are really sure you want to do
this, i'll give you a hint; you want to 'extract' the values of the post
array. If you go this route, please make sure you have read and
understood the warning(s) in the function documentation and followed the
recommended precautions.

In reality though this is generally not a good idea, and almost as bad
as setting on register globals.

Carl.
Isaac Hunt

2006-01-10, 4:00 am

Hi
>
> Yes, but it it not recommended. If you are really sure you want to do
> this, i'll give you a hint; you want to 'extract' the values of the post
> array. If you go this route, please make sure you have read and
> understood the warning(s) in the function documentation and followed the
> recommended precautions.
>
> In reality though this is generally not a good idea, and almost as bad
> as setting on register globals.
>

Thanks for that. Basically I just want to get an old script to run on a new
server so I'll probably go for extract.

On a related subject I'm totally about how I read the 'action'
variable in the following :-

echo "<html><head>
<frameset rows='40,*'>
<frame src='admin.php?action=head' name='head' scrolling='NO'>
<frameset cols='170,*'>
<frame src='admin.php?action=leftframe' name='nav' scrolling='AUTO'>
<frame src='admin.php?action=mainframe' name='main' scrolling='AUTO'>
</frameset>
</head>
</html>";

Any pointers?

--
Isaac Hunt
Hilarion

2006-01-10, 4:00 am

>> Yes, but it it not recommended. If you are really sure you want to do
> Thanks for that. Basically I just want to get an old script to run on a new
> server so I'll probably go for extract.
>
> On a related subject I'm totally about how I read the 'action'
> variable in the following :-
>
> echo "<html><head>
> <frameset rows='40,*'>
> <frame src='admin.php?action=head' name='head' scrolling='NO'>
> <frameset cols='170,*'>
> <frame src='admin.php?action=leftframe' name='nav' scrolling='AUTO'>
> <frame src='admin.php?action=mainframe' name='main' scrolling='AUTO'>
> </frameset>
> </head>
> </html>";
>
> Any pointers?



It goes in the URL, so it's a GET method. Use $_GET['action'] or $_REQUEST['action']
(the other one does not depend on the way the value is passed - it includes
GET, POST and COOKIE).


Hilarion
Jim Michaels

2006-01-15, 9:55 pm

BTW, it's possible to mix GET and POST for the same page. in the form I
specified POST for the method, but I also used url variables. and it worked
with $_GET[] and $_POST[].
probably better to stick with one or the other tho when you can.

"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:dpkpub$qb1$1@news.onet.pl...
>
>
> It goes in the URL, so it's a GET method. Use $_GET['action'] or
> $_REQUEST['action']
> (the other one does not depend on the way the value is passed - it
> includes
> GET, POST and COOKIE).
>
>
> Hilarion



Hilarion

2006-01-16, 9:55 pm

> BTW, it's possible to mix GET and POST for the same page. in the form I
> specified POST for the method, but I also used url variables. and it worked
> with $_GET[] and $_POST[].



Yes, it's possible (I use it quite often). It's sometimes easier to use
$_REQUEST then with the value registration order defined properly.
I also had situations when one name for a value was used in three superglobals:
$_COOKIE, $_POST and $_GET and all three values differed (and it was a valid
case - $_COOKIE had a saved user setting, $_GET has current user setting - not
saved, and $_POST had new value for this setting the user choose to set or
set and save).


> probably better to stick with one or the other tho when you can.


That's probably a good advice.

Do not try to specify some values in URL of action attribute of a form which
has "GET" specified as a method. This will (in many browser) make the resulting
URL invalid (it'll contain two parameter separators: "?" - one specified in the
URL, and second added by the browser, followed by form field values).


Hilarion
Hilarion

2006-01-17, 6:58 pm

>> Do not try to specify some values in URL of action attribute of a form

[color=darkred]
> I am not sure I understand what you are saying (some values?).


This should have been "Do not try to specify ANY PARAMETERS..."
instead of "Do not try to specify some values...".


> are you just saying to avoid having multiple ?s in the URL of the action
> attribute of the <form> tag? if so, I would agree.



I'm not saying that second "?" in an URL (including form action attribute
value) is something invalid in general, because it can be a valid part
of some URL parameter value (in that case it should be encoded as "%3F",
but as "?" it should still be OK).
I'm saying that you should not combine URL parameters in action attribute
of a form and "GET" method in that same form because it can cause the
browser used to submit such a form to create URL containing second "?"
where it should have "&" and in this way append some form input name and
value pair to some action URL parameter value.

If you have a case like that, then you should change form method to "POST",
or move parameters specified in action URL to hidden inputs of the form.


Hilarion
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com