PHP
  Home arrow PHP arrow Page 8 - PHP 101 (Part 2) - Shakespeare's Rose
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (Part 2) - Shakespeare's Rose
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2000-08-08


    Table of Contents:
  • PHP 101 (Part 2) - Shakespeare's Rose
  • Form...
  • ...And Function
  • Operating With Extreme Caution
  • Shakespeare In The Matrix
  • If Not This, Then What?
  • Fortune Smiles
  • Submitting To The King
  • Miscellaneous Notes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP 101 (Part 2) - Shakespeare's Rose - Submitting To The King
    ( Page 8 of 9 )

    You'll have noticed that in all the examples we've shown you thus far, we've used two pages - a single HTML page containing the form, and a separate PHP script which processes the form input and generates appropriate output. However, PHP provides an elegant method to combine those two pages into one via the $submit variable.

    You've already seen that once a form is submitted to a PHP script, all the form variables become available to PHP. Now, in addition to the user-defined variables, each time you hit the SUBMIT button on a form, PHP creates a variable named $submit. And by testing for the presence or absence of this variable, a clever PHP programmer can use a single PHP script to generate both the initial form and the output after it has been submitted.

    Let's demonstrate this to you - we've rewritten the fortune cookie example above to use a single PHP file to generate both the initial drop-down list, and the subsequent fortune cookie page. We're assuming that the PHP file is named "cookie.php4"

    <? if (!$submit) { // if $submit doesn't exist, it implies that the form // has not yet been submitted // so display the first page ?> <html> <head> <style type="text/css"> td {font-family: Arial;} </style> </head> <body> <font face="Arial" size="+2"> The Amazing Fortune Cookie Generator </font> <form method="GET" action="cookie.php4"> <table cellspacing="5" cellpadding="5" border="0"> <tr> <td align="center"> Pick a day </td> <td align="right"> <select name="day"> <option value="Monday">Monday <option value="Tuesday">Tuesday <option value="Wednesday">Wednesday <option value="Thursday">Thursday <option value="Friday">Friday <option value="Saturday">Saturday <option value="Sunday">Sunday </select> </td> </tr> <tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="Hit me!"> </td> </tr> </table> </form> </body> </html> <? } else { // if $submit does exist, the form has been submitted // so process it with switch() // the decision variable here is the day chosen by the user switch ($day) { // first case case "Monday": $fortune = "Never make anything simple and efficient when a way can be found to make it complex and wonderful."; break; // second case case "Tuesday": $fortune = "Life is a game of bridge -- and you've just been finessed."; break; case "Wednesday": $fortune = "What sane person could live in this world and not be crazy?"; break; case "Thursday": $fortune = "Don't get mad, get interest."; break; case "Friday": $fortune = "Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell."; break; // if none of them match... default: $fortune = "Sorry, closed on the weekend"; break; } ?> <html> <head> <basefont face="Arial"> </head> <body> Here is your fortune for <? echo $day; ?>: <br> <b><? echo $fortune; ?></b> </body> </html> <? }
    As you can see, the script first tests for the presence of the $submit variable - if it doesn't find it, it assumes that the form has yet to be submitted and so displays the initial list of days.

    Since the ACTION attribute of the <FORM> tag references the same PHP script, once the form has been submitted, the same script will be called to process the form input. This time, however, the $submit variable will exist, and so PHP will not display the initial page, but rather the page which contains the fortune cookie.

    Note that for this to work, your


    <input type="submit">
    must have a NAME attribute with the value "submit", like this:

    <input type="submit" name="submit">


     
     
    >>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT