Home  >  Article  >  Backend Development  >  protel dxp2004 simplified Chinese cracked version PHP4 practical application experience 8

protel dxp2004 simplified Chinese cracked version PHP4 practical application experience 8

WBOY
WBOYOriginal
2016-07-29 08:33:551008browse

Author: Sun Movement
You may have noticed that in all the examples we have given you so far, we have given you two pages - a simple HTML page with a form, and another to handle the form input and generate a PHP script with corresponding output. However, PHP provides an elegant way to combine those two pages through the $submit variable
  As you already know, once a form is submitted to a PHP script, all form variables become PHP variables. Now, in addition to user-defined variables, a variable named $submit is created every time you click the "SUBMIT" button on the form. Therefore, by testing whether this variable exists, a clever programmer can use only one page to both initialize the form and produce post-submit output.
Let us show you a demonstration - we use one page to implement the fortune cookie example above, including the initial date selection page and the subsequent fortune cookie page. Let's assume that the new PHP file is also called "cookie.php"
------------------------------------------------ --------------------------------------------------
< ?
if (!$submit)
{
// If $submit does not exist, it implies that the form has not been submitted yet
// So the first page is displayed
?>
< 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.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>
< / / If $submit does exist, the form has been submitted
// So use the switch() function to process it
// The decision variable here is the date selected by the user
switch ($day)
{
// The first type Case
Case "Monday":
$ Fortune = "When you can find a complex and exciting method that make everything do, don't make it simple and effective"; :
                                                                                                                                                                                                                                                                                              $fortune = " "; Re BREAK;
Case" Wednesday ":
$ FORTUNE =" What can people who make a sober head live in this world and never get crazy? ";
                                                  break Go with the flow, when you get As you improve you discover that type is a devil.";
                                                                                                                                                              break t;
< 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 whether the $submit variable exists. If it is not found, it will assume that the form was not submitted and display the original date. Select list. Since the ACTION attribute of the < FORM> tag is set to the same PHP script, once the form is submitted, the same script will be called to handle the form input. However, this time the $submit variable will have been called. exists, so the original page will no longer be displayed, but the page with the fortune cookie will be displayed
 Note that in order for these to work properly, your
----------------. -------------------------------------------------- --------------
< input type="submit">
-------------------------- -------------------------------------------------- ------
 There must be a NAME attribute assigned a "submit" value
--------------------------------- --------------------------------------------------
< input type="submit" name="submit">
--------------------------------- --------------------------------------------------
The above introduces protel dxp2004 simplified Chinese cracked version PHP4 practical application experience part 8, including the content of protel dxp2004 simplified Chinese cracked version. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn