Home arrow PHP arrow Page 8 - PHP 101 (Part 2) - Shakespeare's Rose

Submitting To The King - PHP

Now that you've got the basics of PHP variables and operators down, the second article in this series takes a look at PHP's form-processing capabilities, and introduces you to the comparison and logical operators and the "if-else" and "switch" family of conditional statements.

  1. PHP 101 (Part 2) - Shakespeare's Rose
  2. Form...
  3. ...And Function
  4. Operating With Extreme Caution
  5. Shakespeare In The Matrix
  6. If Not This, Then What?
  7. Fortune Smiles
  8. Submitting To The King
  9. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 11
August 08, 2000

print this article


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: