Jump to content

PHP script questionnaire multipage help


FabienO

Recommended Posts

Hello all,I am a psychology student and I need help with creating a questionnaire style script for my data collection. The actual script isn't graded or anything, it's simply the means of getting my results.Basically what I am looking for is an 11 page questionnaire where I can ask on the first page a few different questions about the person and then a little consent tickbox which is necessary if the person was to continue, although thats not an integral part of what I am looking for. The rest of the pages would be a picture displayed first followed by a <form> asking 3 different questions, the only thing that'll change is the picture, all that stuff I can create because im quite good with CSS, the part I need help with is that all the info from each page is at the end 'submitted' and sent to an email address (Which will be mine) so I can see that on Page 1 Question 1 'x' person said 'this', each person doesn't need a number I can tell the difference between the participants.I will show you a basic layout example below.So example: Picture of a baby crying:--------------------How does this make you feel?[Answer Box]What would you do in this situation?[Answer Box]To what degree would you feel angered?[Answer Box]and a link to the next page.--------------------If anyone can show me a script that might accomplish this or perhaps work with me to help create one I would be very grateful.Regards, Fabien.

Link to comment
Share on other sites

Knowing psychology types, you're probably going to want to use a database to temporarily store answers while they are filling it out. You could just use a session instead of the database but the session will expire after 24-minutes of inactivity by default, and once some people get going on how they "feel" they become compelled to write a novel explaining why. So, to eliminate the 24-minute limit on each page it would probably be better to store the user's answers in a database until the end.You would need to do a couple things. First, you need to associate each user with a unique ID, like the session ID, that won't expire. That means it needs to go in a cookie. So on the top of the page you need some code to generate an ID and store it in a cookie, and to check if one is already there:

<?phpif (!isset($_COOKIE['psych_test_id'])){  $id = sha1(time() . mt_rand());  setcookie('psych_test_id', $id, time() + (60 * 60 * 24));}else  $id = $_COOKIE['psych_test_id'];?>

http://www.php.net/manual/en/function.setcookie.phpThat code needs to go before any HTML output on the page or else you'll receive a header error from the server. That will give you a way to track on each page which user is taking the test. Your form needs to include a question number in addition to the answer boxes, so your form might look something like this:

<form method="test.php" method="post">  <input type="hidden" name="question" value="<?php echo $qnum; ?>">  Answer 1: <textarea name="ans1" rows="10" cols="80"></textarea><br>  Answer 2: <textarea name="ans2" rows="10" cols="80"></textarea><br>  Answer 3: <textarea name="ans3" rows="10" cols="80"></textarea><br>  <input type="submit" value="Submit"></form>

That's the basic idea I'm working on, you can map that to what you actually have. The entire test can all be contained in one page, you don't need one page for each question. So you need a way to determine which question you're on. That's related to determining if they submit the form. So you could do something like this to check if they submitted the form, save their answers, and load the next question (or the first question):

<?phpif (isset($_POST['question'])){  $qnum = intval($_POST['question']);  $ans1 = $_POST['ans1'];  $ans2 = $_POST['ans2'];  $ans3 = $_POST['ans3'];  mysql_query("INSERT INTO answers (id, num, ans1, ans2, ans3) VALUES ('{$id}', {$qnum}, '" . mysql_real_escape_string($ans1) . "', '" . mysql_real_escape_string($ans2) . "', '" . mysql_real_escape_string($ans3) . "')");  // next question  $qnum++;}else  $qnum = 1; // default to 1if ($qnum > 11) // they are finished{  $result = mysql_query("SELECT * FROM answers WHERE id='{$id}' ORDER BY num");  $msg = "";  while ($row = mysql_fetch_assoc($result))  {	$msg .= "====================\n";	$msg .= "Question {$row['num']}:\n\n";	$msg .= "Answer 1:\n{$row['ans1']}\n\n";	$msg .= "Answer 2:\n{$row['ans2']}\n\n";	$msg .= "Answer 3:\n{$row['ans3']}\n\n";  }  mail("you@domain.com", "Test results", $msg, "From: results@domain.com");  mysql_result("DELETE FROM answers WHERE id='{$id}'");}?>

http://www.php.net/manual/en/function.mail.phpThat will save the answers, increment the question number, and send you an email if they are finished. Since you have the question number in a variable, this would be an easy way of showing the right image if you named them something like question_1.jpg, question_2.jpg, etc:<img src="question_<?php echo $qnum; ?>.jpg">You'll also need a database table (I called it "answers") with these fields:id varchar(40)num intans1 textans2 textans3 text

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...