Jump to: navigation, search

PHP Multiple Forms

From w3cyberlearnings

PHP Multiple Forms

Multiple forms in a single page is very important when you need user to fill in multiple forms. In each form, users need to fill in information before the other forms can be displayed. The previous entried information are saved in the hidden fields in the current form.

Example TRY-IT


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiple Forms</title>
</head>
<body>
<?php
// use to move from one form to the next
$form = (isset($_REQUEST['form']) && !empty($_REQUEST['form'])) ? $_REQUEST['form'] : 'step1';
$data = "";

if (isset($_REQUEST['name']) && !empty($_REQUEST['name'])) {
	$data = $_REQUEST['name'];
}
switch ($form) {
	case 'step1':
		echo "<form name=\"{$form}\" method=\"post\">";
		echo "<input type=\"text\" name=\"name\"/>";

		echo "<input type=\"hidden\" name=\"form\" value=\"step2\"/>";
		echo "<input type=\"submit\" name=\"sb1\" value=\"step1\"/>";
		echo "</form>";
		break;
	case 'step2':
		echo "<form name=\"{$form}\" method=\"post\" >";
		echo "<input type=\"text\" name=\"name\"/>";
		echo "<input type=\"submit\" name=\"sb2\" value=\"step2\"/>";
		// keep step 1 data
		echo "<input type=\"hidden\" name=\"step1\" value=\"{$data}\"/>";
		echo "<input type=\"hidden\" name=\"form\" value=\"step3\"/>";
		echo "</form>";
		break;
	case 'step3':
		$step = $_REQUEST['step1'];
		echo "<form name=\"{$form}\" method=\"post\">";
		echo "<input type=\"text\" name=\"name\"/>";
		echo "<input type=\"submit\" name=\"sb3\" value=\"step3\"/>";
		echo "<input type=\"hidden\" name=\"form\" value=\"step4\"/>";

		echo "<input type=\"hidden\" name=\"step2\" value=\"{$data}\"/>";
		echo "<input type=\"hidden\" name=\"step1\" value=\"{$step}\"/>";
		echo "</form>";
		break;
	case 'step4':
		echo 'Step 1: ' . $_REQUEST['step1'] . '<br/>';
		echo 'Step 2: ' . $_REQUEST['step2'] . '<br/>';
		echo 'Step 3: ' . $data . '<br/>';
		break;
}
?>
</body>
</html>

Related Links


HTML Form
  1. PHP Login Form
  2. PHP Feedback Form
  3. PHP Register Form
  4. PHP Search Form
  5. PHP Image Upload and MySQL
  6. PHP Multiple Forms
  7. PHP Generate Form by Ajax
Navigation
Web
SQL
MISC
References