Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Store incremented value in questionnaire across multiple pages

Posted on 2013-05-21
17
319 Views
Last Modified: 2013-05-22
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
0
Comment
Question by:b24
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39184489
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
 

Author Comment

by:b24
ID: 39184519
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39184646
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39184659
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
 

Author Comment

by:b24
ID: 39184674
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
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39184729
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39185235
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39185353
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
 

Author Comment

by:b24
ID: 39187703
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39187910
>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
 

Author Comment

by:b24
ID: 39187974
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39188313
>Does anyone else have any suggestions?

Part of my answer was to use javascript/jquery.
0
 

Author Comment

by:b24
ID: 39188348
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39188430
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
 

Author Comment

by:b24
ID: 39188554
Getting the value from the radio button is fine.

I don't know how to build that array with the cookie.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39188670
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39188889
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question