Page 1 of 1
How to POST and process a HTML FORM in one .PHP script!!!
Posted: Thu Jun 16, 2005 5:57 pm
by F1LT3R

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]---|>
?>
Posted: Thu Jun 16, 2005 6:33 pm
by hawleyjr
Welcome to DevNet.
If you use post your code using
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]---|>
Posted: Thu Jun 16, 2005 7:27 pm
by F1LT3R
Thanks dude!
Posted: Fri Jun 17, 2005 12:27 am
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
}
?>
Posted: Fri Jun 17, 2005 5:38 am
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.
NICE!
Posted: Fri Jun 17, 2005 8:41 am
by F1LT3R
Thanks guys, that's great!
