Learn how to create a multi-page form with PHP

Source: Internet
Author: User
Tags functions variable
Creation of course, it's not over here. After all, if you can create multiple forms on a single page, why can't you build a form across multiple pages?

Unfortunately, it's not easy to send data from one page to another page with HTTP. Because there is no built-in mechanism in the HTTP protocol to define the concept of a series of pages. However, with some clever techniques and some useful functions of PHP, we can break through this limitation without too much effort. In this section, the key idea is to save the input from the previous page to the implied variable in the next page form. When we switch between different pages of the screen, the code also passes information between the pages, and each form contains the data from the previous form. This approach seems a bit clumsy, but it's the easiest way to do it right now-without using a database or cookies.

It is expected that we will extend the use of variable $state to accommodate this feature. That's why we've learned about this technique in the past, and we don't intend to work with multiple forms in a single page in a simpler way.

For convenience, we'll use the previous form paradigm again and again. But this time we split it down into two pages, rather than putting the important issue of cheese on the same page as the user name.

This will result in three functions rather than two. In addition, the function is slightly renamed to reflect changes in its content. Display_form () has now been renamed Display_name ().

<?php

function Display_name () {

Global $PHP _self;

?>

<form target= "<?php echo $PHP _self;?>" method=get>

Name: <input type=text name= "name" ><BR>

<input Type=hidden name= "stage" value= "cheese" >

<input type=submit value= "thanks!" >

</FORM>

<?php

}

?>


This is the same as the function described earlier, but let's fix the next question and change the name of the next stage to cheese to more specifically tell the user what to do next.



Next is Display_cheese ().

<?php

function Display_cheese () {

Global $PHP _self;

Global $name;

?>

<form target= "<?php echo $PHP _self;?>" method=get>

Favorite Cheese: <input type=radio name= "Cheese" value= "brie" >very soft French Brie

<input type=radio name= "cheese" value= "cheddar" >farmhouse Chinese cheddar

<input type=radio name= "cheese" value= "mozzarella" >italian Buffalo mozzarella

Favorite Times to Eat Cheese: <input type=checkbox name= "times[]" value= "M" >morning

<input type=checkbox name= "times[]" value= "n" >noon

<input type=checkbox name= "times[]" value= "D" >dinner

<input type=checkbox name= "times[]" value= "L" >late Night

<input type=hidden name= "NAME" value= "<?php Echo htmlspecialchars ($name);?>" >

<input Type=hidden name= "stage" value= "Results" >

<input type=submit value= "thanks!" >

</FORM>

<?php

}

?>

The code above should look very familiar. We did not make any unexpected changes except to output the $NAME variable name and value in the previous part of the data before outputting the hidden stage element. Instead of outputting the variable value directly, we pass a PHP function named Htmlspecialchars (). In HTM, the following four characters are used except as tokens (markup) and should not be used elsewhere: "<", ">", "" ", and" & ". Therefore, to ensure that browsers do not cause confusion, we use the $name variable with the Htmlspecialchars () function. As a result, someone's name "Bret & Jeff" will become "Brett & Jeff".

Now, when this new form is submitted, no information will be lost.

The trick is that we don't have to change any part of the original Process_form () function. In fact, we also keep the function name unchanged. Just add a new line to the page display logic.

<?php

if (empty ($stage)) {display_name ();}

ElseIf ($stage = = ' cheese ') {Display_cheese ();}

else {process_form ();}

?>

Did you see it? We only need to add ElseIf to the previous two statements. This is the ingenious use of variable $stage. If we're going to add a few more pages, just write a new function to show what we want, and then add a line of code to control it when it's displayed.




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.