Page 1 of 1
...on questionnaire...
Posted: Sat Jan 17, 2009 3:46 pm
by al3xandar
I'm new to PHP programming, so I reckon I'm asking for a trivial thing...
I want to design online questionnaire, so I need program that will navigate my respondents from index.php page to let's say test.php page.
Now, my questionnaire consists of several parts (sets of questions). If I put every set of questions in a separate page, my respondents would be able to access them independently. What I would like to do is to have all those parts on a single page e.g. test.php, and when subjects complete one part, program navigates them to second part of test... third part... etc. etc.
I think you've got the picture...
Now, the simple question - how to do it?!

Re: ...on questionnaire...
Posted: Sat Jan 17, 2009 3:53 pm
by Burrito
easiest way:
Code: Select all
<form action="questionaire1.php" method="post">
Code: Select all
<form action="questionaire2.php" method="post">
Code: Select all
<form action="questionaire3.php" method="post">
Re: ...on questionnaire...
Posted: Sat Jan 17, 2009 6:19 pm
by al3xandar
Ummm... Seems like I missed the most important part - I do
NOT want from respondents to access those pages independently!
It's more like questionnaire1,2,3 on test.php...
They click on Next button or something like that, but they
REMAIN on the same page!
So that's the problem!

Re: ...on questionnaire...
Posted: Sat Jan 17, 2009 6:38 pm
by Burrito
Code: Select all
<script>
function showNextPanel(obj,where)
{
np = document.getElementById(where);
np.style.display = "block";
obj.parentNode.style.display = "none";
}
</script>
<div id="panel1" style="display:block">
<input type="text" ... >
<input type="text" ... >
<input type="text" ... >
<input type="text" ... >
<input type="button" onClick="showNextPanel(this,'panel2')" >
</div>
<div id="panel2" style="display:block">
<input type="text" ... >
<input type="text" ... >
<input type="text" ... >
<input type="text" ... >
<input type="button" onClick="showNextPanel(this,'panel3')" >
</div>
Re: ...on questionnaire...
Posted: Sat Jan 17, 2009 7:43 pm
by al3xandar
Whoooa... I knew this was going to be a heavy-duty stuff (at least for noob like me)!
Thanx mate!!!

Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 1:30 am
by Stryks
... or you could avoid using javascript by just embedding a hidden field in each form and have the next form check that the hidden field was submitted. If not, redirect to the first form.
So ... form 1 doesn't check. Form 2 checks that the value is 1, form 3 checks that the value is 2, etc.
I'm just a little reluctant to use javascript without a failsafe for non javascript users.
Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 1:38 am
by RobertGonzalez
Just post back to the same script and break your handling of the segments into parts recognizable in the script. Maintain a session var that tracks where you are in the script and use that to push people along. Also, keeping track of responses along the way allows your user to go to any segment in the form and be able to see their responses before submitting the form.
Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 6:32 am
by al3xandar
Well, I'm more keen on PHP, so I would like to do this entirely with PHP, if possible...
I like the idea that
Stryks suggested: forms that check hidden elements and post to other form. It would be possible to do this through several functions.
Thnx!

Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 2:38 pm
by RobertGonzalez
Hidden fields are available to the end user. If there is any chance that you do not want your users to be able to mess with the data sent along with the form then you might want to consider using sessions. Just a recommendation. Hidden form field elements are also a decent way to go.
Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 3:15 pm
by al3xandar
Everah, would you be so kind and give me an example od that session-thing...

Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 5:33 pm
by Stryks
That's a good point Everah, and I must admit that I do use the sessions method myself.
As an example, on the processing side, just save any values submitted to a session var for form 1 values. Then when you come to the second form, you can check to make sure that the form 1 values exist.
Code: Select all
session_start();
if(isset($_SESSION['form_1'])) {
// this form must be form 2
$_SESSION['form_2']['data'] = $_POST['data'];
} else {
// this form is form 1
$_SESSION['form_1']['data'] = $_POST['data'];
}
Then on form 2, you can just look for verification of form 1 and redirect if it doesn't exist.
Code: Select all
<?php
session_start();
if(!isset($_SESSION['form_1'])) {
header('Location: http://www.yourdomain.com/form_1.php');
exit();
}
?>
Then on the display side of form 1, you can display the previously entered data, if it exists.
Code: Select all
// top of page
<?php
session_start();
if(isset($_SESSION['form_1'])) $preload = $_SESSION['form_1'];
?>
// in form
<input name="data" type="textbox" value="<?php if(isset($preload)) echo $preload['data']; ?>">
This example is all a bit rough and ready and totally untested, but it should get you started. Once you get the idea, you should be able to see how to use a single processing script and redirect to the correct form, etc.
Hope it helps anyhow.
Re: ...on questionnaire...
Posted: Sun Jan 18, 2009 5:35 pm
by RobertGonzalez
Basically what I mean is start a session and show the form. Set a session var to keep track of the segment. Say for example the var starts out at 1. Once a segment is posted and it is validated and ready to move to the next segment, increment the session var and show the next segment of markup for the form. Repeat this until the form is done.