Author: Sun sports
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 PHP script used to process form input and generate corresponding output. However, PHP provides an elegant way to combine the two pages with the $ submit variable.
As you know, once a form is submitted to a PHP script, all the form variables become PHP variables. Now, in addition to User-Defined variables, a variable named $ SUBMIT is created each time you click "submit" in the form. Therefore, by testing whether the variable exists, a smart programmer can use only one page to initialize the form and generate the submitted output.
Let's demonstrate to you-we use a page to implement the above lucky cookies example, including the initial date selection page and the lucky cookies page below. Let's assume that the new php file is also called "cookie. php"
--------------------------------------------------------------------------------
<?
If (! $ Submit)
{
// If $ submit does not exist, it indicates that the form has not been submitted.
// 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. 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.
// Use the switch () function for processing.
// The decision variable here is the date selected by the user
Switch ($ day)
{
// First case
Case "Monday ":
$ Fortune = "do not make everything simple and effective when you can find a complicated and wonderful way to do everything ";
Break;
// First case
Case "Tuesday ":
$ Fortune = "is life a bridge to games? -You must use some clever tricks. ";
Break;
Case "Wednesday ":
$ Fortune = "What makes people with clear minds never go crazy in this world? ";
Break;
Case "Thursday ":
$ Fortune = "don't be crazy, be fun ";
Break;
Case "Friday ":
$ Fortune = "just follow the times and follow the ethos. When you get an improvement, you will find that the type is a devil. ";
Break;
// If none of the above conditions meets...
Default:
$ Fortune = "sorry, the weekend is closed ";
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 whether the $ submit variable exists. If it is not found, it considers that the form is not submitted and displays the initial date selection list.
Because the ACTION attribute of the <FORM> label is set to the same PHP script, once the FORM is submitted, the same script will be called to process the FORM input. However, the $ submit variable after the call will already exist, so the initial Page will not be displayed, but the page with the lucky cookies will be displayed.
Note that your
--------------------------------------------------------------------------------
<Input type = "submit">
--------------------------------------------------------------------------------
You must have a NAME attribute assigned to the "submit" value.
--------------------------------------------------------------------------------
<Input type = "submit" name = "submit">
--------------------------------------------------------------------------------