I have a multiple page form in PHP. I also created a login page in which username and passwords are stored in a session(I used this tutorial). Now, I am a bit confused about these things:
I would like to enable user to fill the form from the last saved session (e.g., in case of computer or browser crash, failed internet connection, etc., he will not rewrite all the data he previously inserted).
Also, I am not sure about how to store data inserted by each user in Mysql table. Is it OK if I just insert all users answers into one table (Let's say "Answers") with these fields: Unique_Id, UserId, question 1, question 2, ... )? Or, it might be better to insert data from each table into a separate table with the same design, for example, table 1: (userId, q1, q2,.., q5), table 2: (userId, q6,q7,..q10) and so on.
This is the code for one of the form pages (Page2, other pages are similar)
<?php
session_start();
// Checking first page values for empty,If it finds any blank field then redirected to first page.
if (isset($_POST['submit1'])){
if (empty($_POST['q1'])
|| empty($_POST['q2'])
|| empty($_POST['q5'])
|| empty($_POST['q6'])){
// Setting error message
$_SESSION['error'] = "Mandatory field(s) are missing, Please fill it again";
header("location: page1.php"); // Redirecting to first page
} else {
foreach ($_POST as $key => $value) {
if (is_array($_POST[$key])){
$_SESSION['post'][$key] = implode(',', $_POST[$key]);
}
else{
$_SESSION['post'][$key] = $value;
}
}
}
} else {
if (empty($_SESSION['error_page2'])) {
header("location: page1.php");// Redirecting to first page.
}
}
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" media=screen>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<div id="show">
<span id="error">
<!---Initializing Session for errors-->
<?php
if (!empty($_SESSION['error_page2'])) {
echo $_SESSION['error_page2'];
unset($_SESSION['error_page2']);
}
?>
</span>
<form id="form2" action="page3.php" method="post">
<div class="meter"><span style="width: 40%">Step 2</span></div>
<fieldset id = "Form_Questions">
<fieldset id = "q8"> <legend class="Q8"></legend>
<label class="question"> What are your favourite genres of movies?<span>*</span></label>
<div class="fieldset content">
<style type="text/css">
.checkbox-grid li {
display: block;
float: left;
width: 25%;
margin-bottom:5px;
}
</style>
<title>Survey Form</title>
<meta http-equiv="content-Type" content="text/html: charset=UTF-8" /
<ul class="checkbox-grid">
<li><input type="checkbox" name="q8[]" value="Action"><label for="checkgenre[]">Action</label><span></span></li>
<li><input type="checkbox" name="q8[]" value="Adventure"><label for="checkgenre[]">Adventure</label></li>
<li><input type="checkbox" name="q8[]" value="Comedy"><label for="checkgenre[]">Comedy</label></li>
<li><input type="checkbox" name="q8[]" value="Animation"><label for="checkgenre[]">Animation</label></li>
<li><input type="checkbox" name="q8[]" value="Drama"><label for="checkgenre[]">Drama</label></li>
</ul>
</div>
</fieldset>
//REST OF QUESTIONS
.....
....
<input class="mainForm" type="submit" name="continue" value="Save and Continue" />
</form>