Home  >  Article  >  Backend Development  >  PHP4 Practical Application Experience (8)_PHP Tutorial

PHP4 Practical Application Experience (8)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:17893browse

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 A PHP script used to process form input and produce 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 the PHP script, all the 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 selection date 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>

< /html>


< ?
}
else
{

// If $submit does exist, the form has been Submitted
// So use the switch() function to process

// The decision variable here is the date selected by the user
switch ($day)
{

// The first case
case "Monday":
$fortune = "When you can find a way to make everything complicated and exciting, don't make it simple and effective";
break;

// The first case
case "Tuesday":
$fortune = "Life is a bridge in the game? - You must have used some kind of trick. ";
break;

case "Wednesday":
$fortune = "What can make a sane person live in this world and never go crazy? ";
break;

case "Thursday":
$fortune = "Don't be crazy, be fun";
break;

case "Friday":
         $fortune = "Just follow the times and go with the trend. When you get promoted, you will find that type is a devil.";
break;


// If none of the above conditions are met...
default:
$fortune = "Sorry, closed on weekends";
break ;

                                                                                                                                                   < /head>

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

< /body>
< /html>

< ?
}
?> ;
------------------------------------------------ ----------------------------------
As you can see, the script first tests the $submit variable exists, if not found, it will assume that the form has not been submitted and display the original date selection list

Since the ACTION attribute of the < FORM> tag is set to the same PHP script, once the form is submitted, the same The script will be called to handle the form input. However, this time the $submit variable will already exist, 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">
---------------------------------- -----------------------------------------------
Required There is a NAME attribute assigned the value "submit"

--------------------------------- -----------------------------------------------
< input type="submit" name="submit">
-------------------------------- --------------------------------------------------------



http://www.bkjia.com/PHPjc/316001.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316001.html

TechArticleAuthor: Sun Sports You may have noticed that in all the examples we have given you so far, we Both give you two pages - a simple HTML page with a form, and another...
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