Why waste pages? Be more network friendly with php
Wednesday, February 25th, 2009
As of June 2008, the index able web contains at least 63 billion pages. On July 25, 2008, Google software engineers Jesse Alpert and Nissan Hajaj announced that Google Search had discovered one trillion unique URLs. Over 100.1 million websites operated as of March 2008. [ wikipedia ]
It's February 25, 2009 now so you can't imagine the number of websites along with their relevant pages that are operating. The main purpose of this article is to show you ways to use the same PHP page for doing many tasks.
Starting with submitting a form:
As you probably know, the syntax is the following:
<form action="foo.php" method="post"> </form>
The key is how you play with the content in " " for the action part. In this case, "foo.php". This is the page the user will be sent to when the submit button is clicked.
Consider the following traditional example which is NOT network friendly:
page1.php will contain the form and page2.php will contain the piece of code that will display the content inserterd in page1.php.
Starting with page1.php:
<form action="<strong>page2.php</strong>" method="post"> First Name: <input type="text" name="fname" /> Last Name: <input type="text" name="lname" /> <input type="submit" name="foo" value="send" /> </form>
Now with page2.php
$fname = $_REQUEST['fname']; $lname = $_REQUEST['lname']; echo "Hello " . $fname ." " . $lname;
Now, consider the following example which will allow you to use only 1 page to accomplish all of the above.
Starting with page1.php:
<form action="" method="post"> First Name: <input type="text" name="fname" /> Last Name: <input type="text" name="lname" /> <input type="submit" name="foo" value="Send" /> </form>
You will realize that i didn't put anything for the action. This means that when the submit button is clicked, it will send the content to the same page. You can put "page1.php" if you like.
Now here's the trick. You need to have a piece of code in that same page that will retrieve the content sent after the submit button. It's one simple "if" statement
Anywhere in the page, use the following:
<?php
if ( $_REQUEST['foo'] == 'Send' ) // you can use any other names instead of "foo" and "Send".
{
$fname = $_REQUEST['fname'];
$lname = $_REQUEST['lname'];
echo "Hello " . $fname ." " . $lname;
}
?>
The same thing can be done using links instead of submit buttons if you don't have any forms. Let's say you wish to show the user new content that will seem like a new page but actually you'll be using only 1 page.
Consider the following traditional example which is NOT network friendly:
<a href="page2.php">page 2</a>
The trick for this is : <a href="page1.php?foo='Send'">page 2</a> and you simply include the same "if" statement above whereby you include anything you want between the brackets.
Most, if not all of us, are addicted to the web. Let's be network-friendly
Happy coding!
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!









Very nice!
glad you like it!
You can also omit the ‘action’ parameter completely — the form will default to sending to the current page.
And something I discovered recently: you can send a form via post to a page with get variables. My mind was blown at the possibilities.
i didn’t know that you can omit the ‘action’ parameter! thanks for the tip!
can you provide a small demo for the second point so i can be sure i understood it?
I still think you should load the parameter foo using $_POST['foo'], this to prevent people from adjusting the http request and putting a foo=Send in the querystring. This will also allow people to set the $_REQUEST['foo'] to send.
I advice everyone to NOT use $_REQUEST. This can cause problems if e.g. a $_GET['foo'] and a $_POST['foo'] exist with different variables. I also know cases when a $_REQUEST variable caused an hour or two of work to debug
True…
i did a quick research and got similar feedback…
As soon as i get the chance, i’ll run a few rests with $_POST and observe the results…
Thanks.
I think i missed a point. Exactly how is it network friendly? The same if not more code gets executed as the page as to check to see if its the first request or not ( so it doesnt error)and also a seperate page would not get indexed in a search if its not linked. I dont see a form submission as a valid link.
I do however think its a good suggestion as its easier to maintain a single page rather than cross page postbacks.
Love and Peace
thanks for this!
by network friendly, i meant to avoid using extra pages and waste space if the same can be achieved using one page.
Nice post! We can also add an extra else condition if someone just want only result not the form like below
First Name:
Last Name:
And also should not use $_REQUEST direct because sometimes it is hard to detect query string variable or post variable.
Opps! my code doesn’t appear.
Thanks for a clear explanation on this, ive not come across this before so its always nice to pick up another bit of helpful advice.
Hey yo, I’m really happy for ya and am gonna let you finish but you didn’t mention PingNinja.com