PHP4 Practical Application Experience (8) _php Foundation

Source: Internet
Author: User
Tags php script fortune cookie generator
Author: Sun Sports

You may have noticed that, so far, in all the examples we have given you, we have given you two pages-a simple HTML page with a form, and another PHP script to process form input and produce corresponding output. However, PHP provides an elegant way to combine the two pages with $submit variables.

As you already know, once a form is submitted to a PHP script, all of the form variables become PHP variables. Now, in addition to user-defined variables, each time you click the "Submit" button on the form, a variable named $submit is created. Therefore, by testing whether the variable exists, a smart programmer can use only one page to both initialize the form and produce the submitted output.


Let's show you-we use a page to implement the cookie example above, including the initial selection date page and the back of the Fortune cookie page. Let's assume that the new PHP file is also called "cookie.php."

--------------------------------------------------------------------------------
<?

if (! $submit)
{
If $submit does not exist, this implies that the form has not yet been submitted
So the first page is displayed

?>

< html>
< head>
< style type= "Text/css" >
td {Font-family:arial;}
</style>

< body>

< font face= "Arial" size= "+2" >
The Amazing Fortune Cookie generator
</FONT>

< form method= "get" action= "cookie.php" >
< 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>



<?
}
Else
{

If $submit does exist, the form has been submitted
So use the switch () function to handle

The decision variable here is the date the user chooses
Switch ($day)
{

The first case
Case "Monday":
$fortune = "Do not make it simple and effective when you can find a complex and wonderful way to make everything work";
Break

The first case
Case "Tuesday":
$fortune = "Life is the bridge of the game?"-you must have some kind of cunning. ";
Break

Case "Wednesday":
$fortune = "What makes the sane person live in this world and never go mad?" ";
Break

Case "Thursday":
$fortune = "Do not be crazy, be interesting";
Break

Case "Friday":
$fortune = "Just follow the times, follow the ethos, and when you get promoted you will find that the type is a demon." ";
Break


If the above conditions are not consistent with ...
Default
$fortune = "Sorry, closed on weekends";
Break

}

?>

< html>
< head>
< Basefont face= "Arial" >

< body>
Here are your fortune for <? Echo $day:
< br>
< b><? echo $fortune;?></b>

</body>

<?
}
?>
--------------------------------------------------------------------------------
As you can see, the script first tests whether the $submit variable exists, and if not, it will assume that the form is not committed and displays the initial date selection list.

Because the < form> tag's action attribute is set to the same PHP script, once the form is submitted, the same script will be called to process the form's input. However, the $submit variable that was called will already exist, so the original page will not be displayed, but the page with the cookie is displayed.

Note that in order for these to work properly, your


--------------------------------------------------------------------------------
< input type= "submit" >
--------------------------------------------------------------------------------
You must have a name attribute assigned to the "submit" value.

--------------------------------------------------------------------------------
< input type= "submit" Name= "Submit" >
--------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.