Is it possible to handle form submission in the same file?

Is it possible to handle form submission in the same file?

am 24.01.2008 00:58:16 von webmaster

If you use Javascript you can set the FORM ACTION to call a function
within the same file (or at least within a *.js file that was
previously loaded into that file). Can you do something similar for
PHP? The code below (which I got from http://www.webmonkey.com) is
supposed to do this, but it doesn't work for me.

So I'm clear on this -- when this runs as there is no variable called
$submit the if true part will not execute, but instead the if false
(=else) part will run instead, showing the HTML FORM. This is indeed
what happens. But I understand that the code in the action attribute
(ie ) will cause the file to be
reloaded. However as the user has clicked the submit button the
variable $submit will now have a value and so the if true part of the
if statement will execute.

It doesn't work for me -- I put data into each of the fields and then
click the button. The data disappears, but I don't get the
'name=value' list that I was expecting.

Is this valid code? If so why isn't it working? If not, what do I need
to do to change it?

Any help will be appreciated.




if ($submit) {
// process form
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value
\n";
}

}else{
// display form
?>


First name:

Last name:

Address:

Position:



}
?>

Re: Is it possible to handle form submission in the same file?

am 24.01.2008 01:38:08 von webmaster

Ok, shortly after posting this I've got my answer (ie code that
works). The only change I've made to the above is to replace

if ($submit) {

with

if(isset($_POST['submit'])) {

and it now works! So please ignore this request.



if(isset($_POST['submit'])) {
// process form
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value
\n";
}
}else{
// display form
?>


First name:

Last name:

Address:

Position:



} // end if
?>

Re: Is it possible to handle form submission in the same file?

am 24.01.2008 10:17:59 von Toby A Inkster

webmasterATflymagnetic.com wrote:

> while (list($name, $value) = each($HTTP_POST_VARS)) {
> echo "$name = $value
\n";
> }

This code looks quite archaic. Does $HTTP_POST_VARS really still exist in
recent versions of PHP?

Use $_POST instead.

And while(list($k,$v)=each($quux)) is an overly verbose foreach loop...

foreach ($_POST as $name=>$value)
{
printf("%s="%s"
\n",
htmlentities($name),
htmlentities($value));
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 24 days, 20:27.]

CSS to HTML Compiler
http://tobyinkster.co.uk/blog/2008/01/22/css-compile/