How to POST and process a HTML FORM in one .PHP script!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
F1LT3R
Forum Newbie
Posts: 3
Joined: Thu Jun 16, 2005 5:25 pm
Location: United Kingdom

How to POST and process a HTML FORM in one .PHP script!!!

Post by F1LT3R »

:D Hey folks,

I've just started learning to program in PHP. Top stuff! Well.. I was going through the book and I was getting rather fed up of having to POST my FORM data to a seperate PHP document for processing. So I came up with this handy little work around.

<? // RETURN TO SENDER
// --------------------------------------------------------------------------------------------------
// Using this method, you do not need to create seperate .HTML and .PHP files.
// You can serve the HTML FORM and process the PHP output in one simple IF statement!
//
// The PHP parser starts by checking weather there is anything in the POSTed [location] variable.
// If there's nothing in there, it must be the first time the PHP script has run... in which case...
// ECHO the HTML FORM. When the SUBMIT button is pressed, the FORM sends the data back to the same
// PHP document. Genius!
//
// Hence - "RETURN TO SENDER"
//
// Once the PHP has been restarted, it now carries with it... the POST variable [location].
// Because this variable is now filled with data, the script takes the opposite route and
// processes the data.
//
// Budda-bing!
//
// (HINT: Be sure to escape and PHP charaters in the ECHO block!)
// EG: echo "\"Hello World!\""


if ($_POST[location] == ""){ // Get the POSTed variable (if one has been posted yet).
echo // If the [location] variable is NULL, ECHO the HTML form.
"<html><head><title>Redirection Menu</title></head><body>
<FORM METHOD=\"post\" ACTION=\"redirect.php\">
<p>Send me to:
<SELECT name=\"location\">
<OPTION value=\"http://www.doctorfilter.com.com/\">Doctor Filter's Personal Website</OPTION>
<OPTION value=\"http://www.somethingfruitful.com/\">Som ... om</OPTION>
<OPTION value=\"http://www.totos.org/\">TOTOS - Child Advocasy Group</OPTION>
<OPTION value=\"http://www.christianvideos.co.uk/\">NPN Videos</OPTION>
</SELECT>
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Go!\"></p>
</FORM>
</body>
</html>"; // Finished ECHOing the HTML FORM.
} else { // If the value of [location] is not NULL...
header("Location:$_POST[location]"); // Print the new HEADER LOCATION.
exit;
}

// A Script by >>>---[F1LT3R]---|>
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Welcome to DevNet.

If you use post your code using

Code: Select all

 and 
it will be more readible.

viewtopic.php?t=21171

Code: Select all

// RETURN TO SENDER
// --------------------------------------------------------------------------------------------------
// Using this method, you do not need to create seperate .HTML and .PHP files.
// You can serve the HTML FORM and process the PHP output in one simple IF statement!
//
// The PHP parser starts by checking weather there is anything in the POSTed [location] variable.
// If there's nothing in there, it must be the first time the PHP script has run... in which case...
// ECHO the HTML FORM. When the SUBMIT button is pressed, the FORM sends the data back to the same
// PHP document. Genius!
//
// Hence - "RETURN TO SENDER"
//
// Once the PHP has been restarted, it now carries with it... the POST variable [location].
// Because this variable is now filled with data, the script takes the opposite route and
// processes the data.
//
// Budda-bing!
//
// (HINT: Be sure to escape and PHP charaters in the ECHO block!)
// EG: echo "\"Hello World!\""

if ($_POST[location] == ""){ // Get the POSTed variable (if one has been posted yet).
echo // If the [location] variable is NULL, ECHO the HTML form.
"<html><head><title>Redirection Menu</title></head><body>
<FORM METHOD=\"post\" ACTION=\"redirect.php\">
<p>Send me to:
<SELECT name=\"location\">
<OPTION value=\"http://www.doctorfilter.com.com/\">Doctor Filter's Personal Website</OPTION>
<OPTION value=\"http://www.somethingfruitful.com/\">SomethingFruitful.com</OPTION>
<OPTION value=\"http://www.totos.org/\">TOTOS - Child Advocasy Group</OPTION>
<OPTION value=\"http://www.christianvideos.co.uk/\">NPN Videos</OPTION>
</SELECT>
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Go!\"></p>
</FORM>
</body>
</html>"; // Finished ECHOing the HTML FORM.
} else { // If the value of [location] is not NULL...
header("Location:$_POST[location]"); // Print the new HEADER LOCATION.
exit;
}

// A Script by >>>---[F1LT3R]---|>
F1LT3R
Forum Newbie
Posts: 3
Joined: Thu Jun 16, 2005 5:25 pm
Location: United Kingdom

Post by F1LT3R »

Thanks dude!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you thought that was handy ;) Here is a way to do it without messing around with escaping quotes etc...

Code: Select all

<?php 
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
  // do stuff
}
else
{
?>

<html>
<head>
<title>hihi</title>
<body>
....
</body>
</html>

<?php
}
?>
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Or (this allows you to put values of variables in too):

Code: Select all

<?php

echo <<<something


your content here

something;

?>
Rules:
First line (with the echo command) must end after the "something" identifier (which can be any alphanumeric string).
The last line must be the identifier followed by a semicolon, and nothing else.
F1LT3R
Forum Newbie
Posts: 3
Joined: Thu Jun 16, 2005 5:25 pm
Location: United Kingdom

NICE!

Post by F1LT3R »

Thanks guys, that's great! :)
Post Reply