dongyu5104 2014-10-03 10:21
浏览 23

以多页php格式保存用户输入数据

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:

  1. 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).

  2. 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>
  • 写回答

1条回答 默认 最新

  • dtkf64283 2014-10-03 14:05
    关注

    If you want to pull answers for a question from a previous visit then it will just be a case of doing a SELECT query on the database to determine what they had entered or leave blank if they have not entered an answer before.

    Cannot work out exactly what you mean by storing all in the session but you might be better storing answers in cookies if you want to save all the answers in one go at the end.

    To make your web page dynamic you could store all questions and answers in the database. You could use a layout like...

    Table name: 'Questions'
      ID               int (autonumber)
      Question         text
    
    Table name: 'Answers'
      ID               int (autonumber)
      QuestionID       int
      Answer           varchar(250)
    
    Table name: 'UsersAnswers'
      ID               int (autonumber)
      UserID           int
      AnswerID         int
    

    (this is assuming all your questions are going to be multiple choice answers)

    Doing something line this will allow you to add as many questions as you like without having to add extra columns to tables.

    Edit...

    If you wanted dynamic answers then you could just store the answers in a table such as...

    Table name: 'UsersAnswers'
      ID               int (autonumber)
      UserID           int
      QuestionID       int
      Answer           string
    

    This table will allow you to store an answer to a question answered by a user.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c