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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway