Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

wwwxxxx.jpeg, xxxxdup, WWWXXX..APC site:board.issociate.de, WWWXXXAPC, WWWXXX .CMD, Wwwwxxx reemine, WWWXXX.VCBA, WWWXXX.VCBA, TheboL.wwwxxxxx, WWWXXXAPC

Links

XODOX
Impressum

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

Posted on 2008-01-24 00:58:16 by 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 <?php echo $_SERVER['PHP_SELF']; ?>) 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.

<html>
<body>

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

}else{
// display form
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
</html>

Report this message

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

Posted on 2008-01-24 01:38:08 by 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.

<html>
<body>
<?php
if(isset($_POST['submit'])) {
// process form
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value<br>\n";
}
}else{
// display form
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>

Report this message

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

Posted on 2008-01-24 10:17:59 by Toby A Inkster

webmasterATflymagnetic.com wrote:

> while (list($name, $value) = each($HTTP_POST_VARS)) {
> echo "$name = $value<br>\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"<br>\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/

Report this message