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!

ABOUT THIS AUTHOR

working . tweeting . reading . blogging . geeking . gaming . sporting . Founder of simplyconfess.com . Personal blog @ http://cli.gs/LpZvbt . twitter: @itsmb , @simplyconfess
  1. Hasasn
    February 26, 2009 at 8:16 pm
    • April 6, 2009 at 4:45 am
  2. February 26, 2009 at 10:38 pm
    • April 6, 2009 at 4:47 am
  3. March 5, 2009 at 1:18 am
    • April 6, 2009 at 5:01 am
  4. Sarah
    March 26, 2009 at 2:21 am
    • April 6, 2009 at 4:50 am
  5. April 17, 2009 at 3:13 am
  6. April 17, 2009 at 3:14 am
  7. July 15, 2009 at 11:14 am
  8. September 17, 2009 at 4:28 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates