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 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.
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.