Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 12:26 PM   PM User | #1
AlexCrow
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
AlexCrow is an unknown quantity at this point
Scored Survey

Hi

I'm trying to create a scored survey for a website - along the lines of 'How many of the following countries have you been to?'. From there the user can click as many as he wants (each worth one point) and there are multiple results depending on how many options the user has selected.

Can anyone show me a template for a survey like this, or give me some pointers as to how to create one. I'm especially having difficulty with the multiple answers, so if anyone could help that would be great.

On a side note I also want the results to be shared via Facebook, eg 'user has been to 43 out of 50 countries', and I have tried the dialogues on Facebook developers but have had limited success.

Thanks a lot

Alex
AlexCrow is offline   Reply With Quote
Old 11-05-2012, 04:48 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Suggest you have a checkbox for each listed country, then add up the total checked, use a switch statement to determine which message is displayed.

I don't understand what you mean by "multiple answers". Do you mean that there are multiple results messages depending on the score?

This ought to move you forward. You could assign differnt "points" values to different countries if you wish. So Germany counts 1 but Australia counts 3. Or whatever.


Code:
<html>
<head>

<style = "text/css">
body {font-family:arial; font-weight:bold;}
</style>
</head>
<body>

<form id = "myform">
USA <input type = "checkbox" name= "chk1" value = "1" >
Germany <input type = "checkbox" name= "chk1" value = "1">
France <input type = "checkbox" name= "chk1" value = "1" >
Australia <input type = "checkbox" name= "chk1" value = "1" >
Japan <input type = "checkbox" name= "chk1" value = "1" >

<input type = "button" value = "Get Total" onclick = "addemup()">
</form>

<script type = "text/javascript">

function addemup() {
var total = 0;
var count = 0;
var f = document.getElementById("myform");
for (var i=0; i<f.chk1.length; i++) {
if (f.chk1[i].checked==true) {
total = total + (f.chk1[i].value*1);
count++;
}
}

if (count == 1) {
alert ("The value of the checkbox you have checked is " + total);
}
else if (count>1) {
alert ("The total of the " + count + " checkboxes you have checked is " + total);
}
}

// then display a message depending on the value of total.  Example:-
if ((total > 0) && (total <4)) {
var message = ("You have not travelled much.");
}

</script>

</body>
</html>
Can't help you with Facebook.


"... liberalism, with a small 'c'....." - David Blunkett MP, Today Programme.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-05-2012 at 04:56 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
AlexCrow (11-05-2012)
Old 11-05-2012, 04:53 PM   PM User | #3
AlexCrow
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
AlexCrow is an unknown quantity at this point
Hi thanks for your suggestions.

By multiple answers, I mean depending on how many boxes you check you go to a different results page which gives you your score and a description eg:

0-10 checks = Bad
10-20 checks = OK
20-30 checks = Good
etc.

Would this be possible with a switch statement??

Thanks
AlexCrow is offline   Reply With Quote
Old 11-05-2012, 04:57 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AlexCrow View Post
Hi thanks for your suggestions.

By multiple answers, I mean depending on how many boxes you check you go to a different results page which gives you your score and a description eg:

0-10 checks = Bad
10-20 checks = OK
20-30 checks = Good
etc.

Would this be possible with a switch statement??

Thanks

Yes, it would. But probably simpler just with a series of if/else statements as there are only a few outcomes. See my suggested code above.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-05-2012 at 05:01 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Old 11-05-2012, 05:07 PM   PM User | #5
AlexCrow
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
AlexCrow is an unknown quantity at this point
One more quick question.

How would I open the results page in a separate tab but obviously link the results to the original checklist page?

Cheers
AlexCrow is offline   Reply With Quote
Old 11-05-2012, 05:10 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AlexCrow View Post
One more quick question.

How would I open the results page in a separate tab but obviously link the results to the original checklist page?

Cheers
Not sure what you mean. Only the user can decide whether to open a page in a new window, the same window or a tab.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-05-2012, 05:13 PM   PM User | #7
AlexCrow
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
AlexCrow is an unknown quantity at this point
My bad, I meant to say a new page not tab.

Instead of the score being a pop up alert, do I just make the 'submit my score' button a link which brings the user to a new page with the results on?

If not how do I get it to come through on a new page?
AlexCrow is offline   Reply With Quote
Old 11-05-2012, 05:18 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You will need to pass the value of the score to a new page using a query string.

For more info see

http://javascript.about.com/library/blqs.htm

Another way would be to use a cookie.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-05-2012 at 05:28 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
AlexCrow (11-06-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:19 PM.


Advertisement
Log in to turn off these ads.