...on questionnaire...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
al3xandar
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:05 pm

...on questionnaire...

Post 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?!
:)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: ...on questionnaire...

Post 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">
 
al3xandar
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:05 pm

Re: ...on questionnaire...

Post 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...
:lol:
They click on Next button or something like that, but they REMAIN on the same page!

So that's the problem!
:(
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: ...on questionnaire...

Post 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>
 
al3xandar
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:05 pm

Re: ...on questionnaire...

Post by al3xandar »

Whoooa... I knew this was going to be a heavy-duty stuff (at least for noob like me)!
8O

Thanx mate!!!
:mrgreen:
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: ...on questionnaire...

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: ...on questionnaire...

Post 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.
al3xandar
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:05 pm

Re: ...on questionnaire...

Post 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!
:D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: ...on questionnaire...

Post 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.
al3xandar
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:05 pm

Re: ...on questionnaire...

Post by al3xandar »

Everah, would you be so kind and give me an example od that session-thing...
:?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: ...on questionnaire...

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: ...on questionnaire...

Post 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.
Post Reply