Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Making a survey using forms

I'm working on a survey for my site.  I have all the layout and forms created and each question has no more than 4 possible answers.  The survey is 7 pages and each input is a radio button with a number value.  I need to carry all the information as they select answers to the next page, and then at the end do a simple math calculation to come with a score.  I know I will need to use CGI and probably JavaScript, but I'm not sure where to begin.  Thanks for your help and let me know if you need additional information.
0
DrPcKen
Asked:
DrPcKen
  • 11
  • 9
  • 6
1 Solution
 
ahoffmannCommented:
each page posts the values to your cgi script, which then shows the next page including the diven values as hidden variables.
This way the last page post all variables, including the ones from previous pages, and your cgi can perform the calculation.
0
 
DrPcKenAuthor Commented:
Ok, I made the forms (radio) and understand how the querystring works when i click submit.  But the form is 8 pages, how do I get the input to carry over when the form is started to when it finishes?
0
 
ahoffmannCommented:
as I said, you script posts the given values as hidden INPUT variables in the next page, and so gets these values with the pages submit button.
Example page1:

<form action=your.cgi method=post>
  <input type=hidden name=page value=1>
  <input type=text   name=val1>
  <input type=submit>
</form>

then the CGI generates page 2:

<form action=your.cgi method=post>
  <input type=hidden name=page value=2>
  <input type=hidden name=val1 value="what ever">
  <input type=text   name=val2>
  <input type=submit>
</form>

and so on ..
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
DrPcKenAuthor Commented:
So I change my form pages from .html to .cgi?  because right now that is what I put so when I click SUBMIT it takes you to the next page.

0
 
DrPcKenAuthor Commented:
This is exactly what it says...

<form action=section2.html method=post>


then when you click SUBMIT, it puts the values of Section1.html into the querystring and takes you to the next page.
0
 
ahoffmannCommented:
> .. <form action=section2.html method=post>
duh, does not make sence (on most web servers), except with special (in case loosy) configuration.
.html is a static file, usually, and does not process input (except section2.html is a program **and** the server accepts it as program).

you need:  <form action=your.cgi method=post>
0
 
DrPcKenAuthor Commented:
Ok I see your point.  So how would I make the your.cgi file?
0
 
ahoffmannCommented:
choose you favorite programming language, then it goes as follows (convert to appropriate syntax):

  read posted data from STDIN;
  split result at & and store in a list;
  loop above list {
    split each list-value at = into name and value;
    store name and value;
  }
  increment value of accociated with name "page";
  if ($page lower-to your-last-page) {
    print "Content-type: html/text\n\n";
    print --whateverHTMLyouneed--;
    print "<form action=your.cgi method=post> ";
    print "<input type=hidden name=page value=$page>"
    loop over list of name-value-pairs {
      print "<input type=hidden name=$name value=$value>";
    }
    print --whateverothervaluesyoulike--;
    print "<input type=submit></form>";
  } else {
    process your data (see name-value pairs above);
  }
0
 
chapattiCommented:
Hi DrPcKen,

You could carry the values forward with javascript also.

Change the form method to get and the value get carried forward in the url such as section2.html?firstq=2&secondq=3 etc.

Now on section2.html you parse the query string as follows:

function PageQuery(q){
     if(q.length > 1) this.q = q.substring(1, q.length);
     else this.q = null;
     this.keyValuePairs = new Array();
     if(q){
          for(var i=0; i < this.q.split("&").length; i++){
               this.keyValuePairs[i] = this.q.split("&")[i];
          }
     }
     this.getKeyValuePairs = function(){ return this.keyValuePairs; }
     this.getValue = function(s){
          for(var j=0; j < this.keyValuePairs.length; j++){
               if(this.keyValuePairs[j].split("=")[0] == s){
                    return this.keyValuePairs[j].split("=")[1];
               }
          }
          return -1;
     }
     this.getParameters = function(){
          var a = new Array(this.getLength());
          for(var j=0; j < this.keyValuePairs.length; j++){
               a[j] = this.keyValuePairs[j].split("=")[0];
          }
          return a;
     }
     this.getLength = function(){ return this.keyValuePairs.length; }    
}

Now you can access the Name/Value pairs:

     var NAME = page.getValue("name");

On the second page you add the values from the first page in hidden tags and do the get-thing again and parse the new set of values again, etc.

However, for the last form you will have to use a cgi script to forward the info.

TTFN,

chapatti
0
 
DrPcKenAuthor Commented:
Hmmm, I like the Javascript.  Ok, here is a snippet of code to help you guys see how my forms are working. This is question 1 of Section 1. Also, is there anything I'd have to add to that javascript above, or is that all I need?

<FORM ACTION="section2.html" METHOD="get" NAME="section1">
<BLOCKQUOTE><H2>1. How concerned are you about the safety and security of your company's critical information?</H2>
<TABLE WIDTH="25%" BORDER="0" CELLPADDING="0">
  <TR>
       <TD NOWRAP><p><INPUT TYPE="Radio" NAME="s1q1" Value="5"> Very concerned <I>(5 points)</i></P></TD>
  <TR>
     <TD NOWRAP><P><INPUT TYPE="Radio" NAME="s1q1" Value="3"> Somewhat Concerned <I>(3 points)</i></P></TD>
  <TR>
     <TD NOWRAP><P><INPUT TYPE="Radio" NAME="s1q1" Value="1"> Not very concerned <I>(1 point)</i></P></TD>
  <TR>
     <TD NOWRAP><P><INPUT TYPE="Radio" NAME="s1q1" Value="0"> Not concerned <I>(0 points)</i></P></TD>
</TABLE></BLOCKQUOTE>
0
 
chapattiCommented:
That would be all you need to parse the querystring however you would have to fill in the hidden tags with javascript again so that you get the "page.getValue("name")" into the tag to get forwarded onto the next page.

Maybe you can even forward them within the form tag such as:

<FORM ACTION="section2.html?s0q1=4&s0q2=1&..." METHOD="get">

... in this case you would have to write the form tag line in javascript:

var test1 = page.getValue("s0q1");
var test2 = page.getValue("s0q2");

document.write('<FORM ACTION="section2.html?s0q1=' + test1 + '&s0q2=' + test2 + '&..." METHOD="get">');
0
 
DrPcKenAuthor Commented:
ahhhh
This is starting to make more sense!

Thanks alot!
I'll try some of this and let you guys know.
0
 
ahoffmannCommented:
1. JavaScript does not solve the problem, at least if the browser does not support it, for any reason
2. I still do not see how you get the values with GET into your section2.html
  could you please explain?
0
 
DrPcKenAuthor Commented:
so instead of GET i should use POST?
0
 
DrPcKenAuthor Commented:
From my understanding, GET makes the querystring viewable in the URL, POST hides the values from the user.  
0
 
chapattiCommented:
ahoffmann,
In regards to 1.
you are correct, it will not work if javascript is not enabled but this does not happen very often as it is turned on by default on the major browsers.

DrPcKen,
What is your target audience? Would they use text only browsers or turn off their javascript?

ahoffmann,
In regards to 2.
The "get" method will append the name / value pairs to the url thus making it possible for javascript to access.
Maybe it would work as well with the "post" method - I just haven't tried this yet.

0
 
DrPcKenAuthor Commented:
I have no doubt that the target audience using this form will either be using IE or NETSCAPE (blah).  

The GET method stores the values in a visible querystring.  I'm not worried about this being displayed though.
0
 
ahoffmannCommented:
hmm, didn't think of such a sophisticated function(), nice idea chapatti ;-)
0
 
chapattiCommented:
Thanks ahoffmann!
0
 
chapattiCommented:
Thanks ahoffmann!
0
 
DrPcKenAuthor Commented:
When I start the javascript (on section2 right?) i start the java script in the <HEAD> tag after the <SCRIPT="JavaScript"> Tag right?
0
 
DrPcKenAuthor Commented:
BTW, at the end of each section, it will have too add the value of all the questions together, divide by the total possible points (which is 50 in section1) and then multiply by 100.  

That math problem gives a value that needs to be carried over to the next page, and when all the values are gotten, it will ADD all the values from each section, divide by the number of sections completed and that equals your total score.

i'm still a bit confused about some of it :(
0
 
chapattiCommented:

hey DrPcKen,

am currently a bit busy but will help you when i am done.

later,

chapatti
0
 
chapattiCommented:

i am back now. just putting together a sample page for ya.

will let you know when done.

chapatti

0
 
chapattiCommented:

me again,

here's the url to the test pages:
http://www.themediaco.com/testing/index.html

look at the source code to see how it's done.

what's left to do is, to check for empty radio boxes (if the user forgets to select an answer):
if(s1q1 != "-1"){
  // proceed
}else{
   // error message
}

cheers,

chapatti
0
 
chapattiCommented:
Thanks for the points and the A!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 11
  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now