Need Javascript Code for Needs Analysis Survey

  • BSquared18
  • Born
  • Born
  • BSquared18
  • Posts: 2

Post 3+ Months Ago

Hi,

I’ve looked without success on various sites for some Javascript code to accomplish the following task:

For simplicity, let’s assume a three-item yes/no “needs analysis” questionnaire. (There actually will be several items, maybe a dozen or so.) The user clicks “YES” or “NO” for each questionnaire item (maybe a radio button) and then clicks a button to get the results.

Each questionnaire YES/NO item has its own response paragraph, but only the paragraphs for the items the user marks “YES” are shown to the user when the “results” button is clicked. Thus, the information shown is tailored according to how the user answered the questionnaire items.

There's no need to save the data. It's for the user's own use.

For example:

I smoke or chew tobacco. x YES o NO

I eat a lot of fatty foods. o YES x NO

I am more than 20 pounds overweight. x YES o NO

"Results" Comments:

We have a stop-smoking/chewing program that is guaranteed to help you quit in six weeks or less.

We have a weight-reduction program that can be tailored to your particular needs.

(The actual questionnaire items won’t be on the topics shown. These are for illustration purposes only. Note that no information is given on fatty foods, since that item was checked “NO.”)

Thanks!

Bill B.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

rather use a PHP script (or ASP depending on the server). Lot of people are turning JavaScript off.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Anyone who actually turns off JavaScript is probably also computer saavy enough to realize many sites will fail. I don't feel sorry for them.

BSquared18, this seems simple enough... I'll give it a shot and post what I come up with in a bit.

.c
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Although I have noticed that a few of these 'lock your computer up on the internet' type application do it as well without the consent of the user
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

True. Then again, people should pay more attention to things like that. If they're going to install an internet lockdown system, it's not a stretch to assume that this installation might cause sites to not work right... I still don't feel sorry for them. I like using JavaScript, it reduces server load, and when written proplerly, it increases site efficiency. Doing everything on the server has perfomance drawbacks, since you're doing everything on the server...

At some point the LCD (lowest common denominator) has to be forced upwards for the good of all... imagine if NS 4.01 was still the LCD...

.c
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

This is also true (NS4)

I don't have anything against javascript and I agree that anything that can be processed on the client side should be, it is just that alot of people who know nothing about it sometimes write very hiccupy code and don't optimise it correctly.

Suppose the best solution is to write IE specific code and NS specific code and have a server side script include the correct one.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

RD
Sadly, that's probably the most efficient.. although these days the DOMs are getting much more similiar


BSquared, try this (I tested it on FireFox and IE 6, on PC, nothing else... I'll leave that up to you)



**EDIT: I thought of a couple ways to do this using arrays or switch statements that might have been more efficient, but it would have been considerably more difficult and taken longer to write. This works more or less the same though.

Code: [ Select ]

<html>
<script LANGUAGE="javascript" TYPE="text/javascript">
    function results(form_obj,element_obj){
        smoke_value = get_radio_value(form_obj.smoke)
        eat_value = get_radio_value(form_obj.eat);
        overweight_value = get_radio_value(form_obj.overweight);

        if(smoke_value == 1){smoke_result = "We have a stop-smoking/chewing program that is guaranteed to help you quit in six weeks or less.";}
        else{smoke_result = "";}

        if(eat_value == 1){eat_result = "Go eat some lettuce, porker.";}
        else{eat_result = "";}

        if(overweight_value == 1){overweight_result = "We have a weight-reduction program that can be tailored to your particular needs.";}
        else{overweight_result = "";}

        content = "<p>" + smoke_result + "<p>" + eat_result + "<p>" + overweight_result;

        results_frame.document.write(content);
        results_frame.document.close();

        return false;
    }

    function get_radio_value(element_obj){
        for(x=0;(x<element_obj.length);x++){
            if(element_obj[x].checked){ return element_obj[x].value; }
        }
        return false;
    }

</script>

<p><table>
<form ACTION="#" METHOD="POST" NAME="surveyform">
<tr><td>I smoke or chew tobacco:</td><td><input TYPE="radio" NAME="smoke" VALUE="1"> yes</td><td><input TYPE="radio" NAME="smoke" VALUE="0"> no</td><tr>
<tr><td>I eat a lot of fatty foods:</td><td><input TYPE="radio" NAME="eat" VALUE="1"> yes</td><td><input TYPE="radio" NAME="eat" VALUE="0"> no</td><tr>
<tr><td>I am more than 20 pounds overweight:</td><td><input TYPE="radio" NAME="overweight" VALUE="1"> yes</td><td><input TYPE="radio" NAME="overweight" VALUE="0"> no</td><tr>
<tr><td COLSPAN="3"><input TYPE="button" VALUE="submit" NAME="submit" onClick="results(this.form,this);"></td></tr>
</form>
</table>
<p><iframe SRC="" NAME="results_frame" ID="results_frame" ALIGN="top" HEIGHT="200" WIDTH="100%" ALIGN="middle" SCROLLING="no" FRAMEBORDER="0"></iframe>
</html>
  1. <html>
  2. <script LANGUAGE="javascript" TYPE="text/javascript">
  3.     function results(form_obj,element_obj){
  4.         smoke_value = get_radio_value(form_obj.smoke)
  5.         eat_value = get_radio_value(form_obj.eat);
  6.         overweight_value = get_radio_value(form_obj.overweight);
  7.         if(smoke_value == 1){smoke_result = "We have a stop-smoking/chewing program that is guaranteed to help you quit in six weeks or less.";}
  8.         else{smoke_result = "";}
  9.         if(eat_value == 1){eat_result = "Go eat some lettuce, porker.";}
  10.         else{eat_result = "";}
  11.         if(overweight_value == 1){overweight_result = "We have a weight-reduction program that can be tailored to your particular needs.";}
  12.         else{overweight_result = "";}
  13.         content = "<p>" + smoke_result + "<p>" + eat_result + "<p>" + overweight_result;
  14.         results_frame.document.write(content);
  15.         results_frame.document.close();
  16.         return false;
  17.     }
  18.     function get_radio_value(element_obj){
  19.         for(x=0;(x<element_obj.length);x++){
  20.             if(element_obj[x].checked){ return element_obj[x].value; }
  21.         }
  22.         return false;
  23.     }
  24. </script>
  25. <p><table>
  26. <form ACTION="#" METHOD="POST" NAME="surveyform">
  27. <tr><td>I smoke or chew tobacco:</td><td><input TYPE="radio" NAME="smoke" VALUE="1"> yes</td><td><input TYPE="radio" NAME="smoke" VALUE="0"> no</td><tr>
  28. <tr><td>I eat a lot of fatty foods:</td><td><input TYPE="radio" NAME="eat" VALUE="1"> yes</td><td><input TYPE="radio" NAME="eat" VALUE="0"> no</td><tr>
  29. <tr><td>I am more than 20 pounds overweight:</td><td><input TYPE="radio" NAME="overweight" VALUE="1"> yes</td><td><input TYPE="radio" NAME="overweight" VALUE="0"> no</td><tr>
  30. <tr><td COLSPAN="3"><input TYPE="button" VALUE="submit" NAME="submit" onClick="results(this.form,this);"></td></tr>
  31. </form>
  32. </table>
  33. <p><iframe SRC="" NAME="results_frame" ID="results_frame" ALIGN="top" HEIGHT="200" WIDTH="100%" ALIGN="middle" SCROLLING="no" FRAMEBORDER="0"></iframe>
  34. </html>
  • aycaramba!
  • Born
  • Born
  • aycaramba!
  • Posts: 2
  • Loc: Ipswich, UK

Post 3+ Months Ago

Hi folks,

I've also got a similar request to BSquared18. I've just started looking into coding with Javascript and obviously have a lot to learn. Would anyone be able to help with the following requirement that I've got for my site?

Basically, I have an online lifestyle survey which the user completes. The results are purely for their eyes only - no data is sent anywhere.

There are two blocks of simple questions, each with a yes or no radio button answer. What I want to do is to have the user enter their choice for each radio button and then click submit after answering all the questions in the block. On submit, the javascript should total up the number of no's for the first block of questions and write an answer to the web page depending on how many no's. There are only 3 possible answers.

For the second block of questions, there are only two possible answers, this time dependent on how many yes answers the user gives again on submit.

I've tried searching the web for some guidance on how to go about this but have not had much luck. As I mentioned earlier, I'm learning javascript but unfortunately this particular requirement is beyond me at the mo!

For more info on the survey itself and the answers that need to be specified have a look at the page itself at http://www.secondincome.ws/survey.htm.

Any help would be much appreciated.

Cheers,

Aycaramba!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Um well if you are going to be doing this across multiple pages you are going to have to maintain state between each page.

Alot of people will probably flame me for this but I would recommend going with server side and session variables. It isn't very scalable but it is probably the best way to maintain state. (unless of course you write cookies - which would probably be a better way of doing it to reduce load on the server.).

I for one am a bit worried about using javascript especially now after the recent scare of a new virus using javascript. The sad thing is that users will treat the symptoms by disabling javascript and not the problem.

Anyways that is my two cents. If I find the uri for the article I will post it here
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

State in this case is simple to maintain, since there's only three total pages. (two questions, and a results display). With either JS or a server-side language, you can do the whole thing on a single page. Since there isn't any data being stored, I'd suggest simply holding state by writing the answers from the first set of questions to hidden form fields, then the results will tabulate the whole thing at once.

What sort of server are you using? Do you have the ability to use server-side languages? (ASP, PHP, JSP, ColdFusion)

.c

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 144 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.