Store incremented value in questionnaire across multiple pages

Hi there,

I'm trying to set up a simple questionnaire that spans a number of html pages (i.e. ten questions). Each radio button represents a number (1 to 5) and I want to be able to store that number (when they click Submit) into a global variable and then present it back to them on the final page. I was hoping to do this with javascript rather than php.

Does anyone know how I might set this up?

Thanks in advance.
form.html
b24Asked:
Who is Participating?
 
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
Arrays http://www.w3schools.com/js/js_obj_array.asp
Cookies http://www.w3schools.com/js/js_cookies.asp

On question one you will start with a variable lets call results where you will store the answers.

var results='';

Then once you get the value from the form field

results=results+'"'+val_from_question+'",';

When you grab the very last question leave out the comma at the end.  You want to end up with the variable results looking like:  "2","1","4","3","3","2"

Then when you need to loop through, you can do: var MyArray= New Array(results);

To pass the results from page to page, after the selection and saving results=results+'"'+val_from_question+'",'; you set the cookie. Then on the next page, the first thing is to re declare the variable results and make it the value of the cookie you set on the previous page.
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The only way to go from page to page is leave cookies or keep the answers in the url.  Probably a better option is to do everything on the same page but give the illusion of multiple pages like a wizard or tabs.  http://thecodemine.org/#
0
 
b24Author Commented:
Thanks padas. They have to be on separate pages for a whole heap of reasons. I'd be open to cookies though. We're using them elsewhere so we're already declaring their use. Any idea how I might go about setting that up?

Thanks for the reply.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
js cookies http://www.w3schools.com/js/js_cookies.asp  or jquery https://github.com/carhartl/jquery-cookie

I use jquery in just about everything and am used to that.
0
 
COBOLdinosaurCommented:
Why would you want to tie yourself in knots with javascript when PHP would let you use sessions and the whole thing would be smooth, transparent, and robust?

Cd&
0
 
b24Author Commented:
Hi COBOLdinosaur,

Thanks for pointing that out. Do you have any examples of something like this (a value that increments) working with a php session?

My preference was javascript because I use it more often but perhaps I should just bite the bullet and use php.

Any help would be greatly appreciated.
0
 
COBOLdinosaurCommented:
Sessions are just global variables.  When the user comes to the page you do a session_start() to create the $_SESSION array and then you can store whatever you need, reference whatever is there, and move information across pages for the term of the session. As long as the user remains active, the session remains active.

All you really need is pretty basic and this should give you what you need:

http://www.w3schools.com/php/php_sessions.asp

Cd&
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Sessions or cookies, I agree to use serverside instead of javascript.     The only thing to watch for with sessions is to account for the session starting over/loosing the current session.  Maybe it is just a windows thing but if something on the server max's out for a short time, the session can start over.    

Since sessions can eat up memory, I would store the answers in an array after each answer/page, then access the array at the end to update the db or display. I am not a php developer but it would be something like this after each question.

$_SESSION["results"]=""
$_SESSION["results"]=$_SESSION["results"].",".$_POST["question_1"];  // keep adding on to the session like a csv file
:
:
$_SESSION["results"]=$_SESSION["results"].",".$_POST["question_10"];

Then when you are ready to update your database or display results you would create an array out of the session and loop through it.  I think like this

$MyArray=array($_SESSION["results"]);

foreach ($MyArray as $answer)
  {
    //  do something
  }

That is how I do it on the MS side.
0
 
b24Author Commented:
Sorry guys,

I've been going around in circles all day between using cookies and using php sessions. The pages with the forms on them have to remain html not php pages i.e. page1.html.

Does anyone have a working example of one of these options incrementing a value across html pages?

I would post code but it's a terrible mess at this stage.

Any help would be hugely appreciated!
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
>have to remain html not php
Is it because you are having problems with php?

Just take it one step at a time.  I would start with just posting a variable and accepting the data and sending it to the screen.  You can ask separate questions for each step then put it together.

Javascript is not any easier then php, it just runs on the client (person using the site) rather then the server.  It can also be manipulated because it does run on the client.  Or javascript could be turned off and then you get nothing.

Actually, I think javascript is going to be even more complex especially if you are not familiar with it.  If you do go the javascript way, I would keep it all on one page, and just make each question on a different div something like <div id="1">Question one 1 </div><div id="2">Question one 2 </div>.  Then hide and open divs based on which question you are on.

Otherwise, to go from page to page, you will make each form a get so it posts to the url.  Starting with question/page 2, have a hidden form field called "results" that accepts the url called answer.  It would look like results=results+answer.

There are tutorials on php in many places http://www.w3schools.com/php/php_post.asp http://www.codecademy.com/learn  start with creating a form and posting data to a page and display... if you have problems, start a new question here.
0
 
b24Author Commented:
Thank you padas. The reason they have to be in HTML is because there are approximately 20 other static pages that don't have forms of this nature on them and I don't want to have to redo all of those in php. Nor do I want to randomly jump to php half way through the site. I am much more familiar with JavaScript.

Does anyone else have any suggestions?

Many thanks.
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
>Does anyone else have any suggestions?

Part of my answer was to use javascript/jquery.
0
 
b24Author Commented:
Apologies. I'm really looking for an example of something like this working. Could you help me with a javascript function that stores the chosen value of the radio button in a cookie and increments that value when I move between pages and answer other questions?

Something like this maybe?
cookie.html
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Now you just need to grab the value of the radio.  Then send the value to  the url.  

I am better using jquery then all js.  

Keep one variable as an array you keep building on with each question.

This should help with getting form field data http://www.javascript-coder.com/javascript-form/getelementbyid-form.phtml
0
 
b24Author Commented:
Getting the value from the radio button is fine.

I don't know how to build that array with the cookie.
0
 
COBOLdinosaurCommented:
I don't want to have to redo all of those in php

You don't have to. PHP can be used as the default parser, and .html pages that do not contain php will not be affected;  and you can have PHP pages with an .html extension.  All it takes is a directive in the .htaccess file.

However if you are more comfortable in javascript then the direction that padas is showing you, should get you where you want to go.

Cd&
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.