Webforumz Newsletter - January 2008

Tutorials

Multiple Step Form Entry Using PHP

Introduction

Many large web sites use multiple step form entry to gather information from customers. For example, when signing up to a web site, the first page may have you enter your username and password, while the pages following would have you enter your address, phone number, zip code, and other details.

Today, we are going to learn how to create this type of successive form entry. More importantly, we will accomplish this using only one PHP page.

How to Do It

Declare the start of PHP code followed by an IF statement that tests to see if a form button with name "submit1" has been pressed.


<?php
if(isset($_POST['submit1']))
{

Take the values passed from the form fields and assign them to variables.


$name = $_POST['name'];
$age = $_POST['age'];
$color = $_POST['color'];

We can now echo these variables to the page.



echo ($name." is ".$age." years old and his favorite color is ".$color);

Create a form. Notice that we can place it within the PHP code by using the echo function. Also notice that this submit button has the name "submit2". End the IF statement.


echo ("<form action='".$PHPSELF."' method='post'>Type a number: 
<input name='number' type='text'><input name='submit2'
type='submit' value='submit'></form>");
}

Create an ELSE IF statement that will test if a form button with name "submit2" has been pressed. Include, once again, the variable passed and an echo printing that variable to the page. Don't forget to end the ELSE IF statement.



else if(isset($_POST['submit2']))
{
$number = $_POST['number'];

echo ("A number: ".$number);
}

Start the ELSE statement followed by the closing tag for the PHP code. Because the ELSE statement has not been closed, everything below the PHP closing tag is still contained within the ELSE statement even though it is not inside the PHP code. This method may look strange, but it allows easier HTML coding without the need to use echo.


else
{
?>

Create another form using HTML, setting the action to $PHPSELF which refers to the current page. This submit button has name "submit1".


<form action="<?php echo $PHPSELF ?>" method="post">
name: <input name="name" type="text">
<br>
age: <input name="age" type="text">
<br>
favorite color: <input name="color" type="text">
<br><br>
<input name="submit1" type="submit" value="next step">
</form>

Finally, restart the PHP code, end the ELSE statement, and end the PHP code.


<?php
}
?>

How it Works

The first thing that the user sees upon loading the page is whatever is contained in the ELSE statement since neither the IF or the ELSE IF statements are true (no button with the name "submit1" or "submit2" has been pressed yet). The user fills out the form and presses the submit button. Because the action of the form is set to the current page, it reloads the page except this time, the button "submit1" has been pressed so it displays whatever is within the IF statement that tests for that. So, the user fills out more information on this form and presses the submit button, once again reloading the page. However, this time, the button "submit2" has been pressed, displaying whatever is within the ELSE IF statement, since it tests whether that button has been pressed.

The Final Code

<?php
if(isset($_POST['submit1']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$color = $_POST['color'];

echo ($name." is ".$age." years old and his favorite color is ".$color);
echo ("<form action='".$PHPSELF."' method='post'>Type a number: <input
name='number' type='text'><input name='submit2' type='submit'
value='submit'></form>");
}

else if(isset($_POST['submit2']))
{
$number = $_POST['number'];

echo ("A number: ".$number);
}

else
{
?> <form action="<? echo $PHPSELF ?>" method="post">
name: <input name="name" type="text">
<br>
age: <input name="age" type="text">
<br>
favorite color: <input name="color" type="text">
<br><br>
<input name="submit1" type="submit" value="next step">
</form>
<?php
}
?>