Search code examples
phpmysqlformspdomultipage

enable user to start form the last saved page


I have a PHP form in 5 pages and I would like to give users the possibility of start filling the form from their last saved page. (data should be saved into DB per page and if for any reason a user could not finish all pages, he would be able to continue instead of re-filling all pages again). These are the steps that I am trying to do:

  1. Save user data per page, so when he click on "save&continue" button at the end of the page, all entered data will be saved in DB (THIS PART IS OK, I already did this step)

  2. When user login successfully, I check the table "answer" in DB to see if user has already entered some data (In each page I have some mandatory questions, so I check if they are empty in DB, for example: "q6" in page1 is required field and if this field (column q6) in DB is NOT "empty" it means that user already answered the first page and he should be able to start from second page. Then I will check for another mandatory field in the next page e.g q12, if the column "q12" in table is not empty, it means that user also answered the second page and he can start from the 3rd page , and so on...

Here is part of the code: (To make the code shorter, I just copied the condition to check if user answered the first page and I omitted other conditions to check for empty fields in next pages)

// If username& password found in DB, LOGIN SUCCESSFUL
$_SESSION['SESS_MEMBER_ID'] = $result->mem_id;
$_SESSION['SESS_USERNAME'] = $result->username;
$_SESSION['SESS_LAST_NAME'] = $result->password;

$result = $conn->prepare('SELECT * FROM answer where username = :username');
$result->execute(array(':username' => $username)); 

$row = $result->fetch(PDO::FETCH_OBJ);

if(empty($row->q6)){  //if user didn't answer q6 (which is required to submit the first page and go to 2nd page                 
  try{ 
     $stmt2 = $conn->prepare('INSERT INTO answer(username) VALUES(:username)');
     $stmt2->execute(array(':username' => $username));

   }catch(PDOException $e) {
       echo 'Error: ' . $e->getMessage();
   }
header("location: home.php"); //homepage where user can click on the link to start filling the form in page1.php
exit();

}else{  //If q6 had already an answer in database, it means that user filled and saved the first page before, so he should be able to continue form the second page
  header("location: page2.php");
   exit();
}

  }else {
  //Login failed
   $errmsg_arr[] = 'user name and password not found';
   $errflag = true;
   if($errflag) {
   $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    header("location: index.php");
    /*  exit();*/
    }
  }

PROBLEM:

It works perfectly for the user who is filling the form for the first time (his username is entered in table "answer" when he logins and he will be redirected to home.php where he can find the link to start filling the form (page1.php), but the problem is that if for example he fill the first page, click "save&continue", he will be redirected to second page and if he quit the form (e.g if he accidentally close the window), and login again, he is redirected to page1.php insead of page2.php

Could someone please kindly help me what is the problem with this code?

Thanks,


Solution

  • YESSSSSSSSS, I found it :DD

    Problem: I checked the values of the first page at the beginning of page2.php and if it found an empty field(required fields), it redirected to page1.php and It was exactly the point..(since when user who already filled the first page, if logins for the second time, he has to be able to continue from the 2nd page, but this check disturbed everything..

    Solution: So, I just used jquery validation plugin at the end of each page.. and deleted the validation check part that I had at the beginning of next pages (e.g, validate q1-q7 at the end of page1.php and deleted check part at the beginnig of page2.php...so, it didn't redirect to page1.php any more and now user can continue from the last saved page :) (I hope I could explain clearly for those who have similar problem)