Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: nend on April 09, 2016, 06:49:37 PM

Title: [TIP/TRICK] Surveys, Like what ___ are you.
Post by: nend on April 09, 2016, 06:49:37 PM
Well I was thinking of writing a mod, but then again I wanted the surveys in my topics.

I started the HTML and JavaScript, missed around with that for awhile and came up with the basic workings. While looking at this I figured it was simple enough where a modification is not needed and one could use just the forum system.

First you need to put in the JavaScript. Place this in script.js in the default theme as this would make it available to most themes.
function sisurvey(form,answers) {
var sirespon = answers.split(',');
var siresult = 0; var lrg = 0; var sicount = new Array();
for (i=0; i<sirespon.length; i++)
sicount[i] = 0;
for (i=0, fl=form.elements.length; i<fl; i++)
if (form.elements[i].checked)
sicount[form.elements[i].value]++
for (i=0; i<sicount.length; i++)
if (sicount[i]>lrg) {
var lrg=sicount[i];
var siresult=i;
}
document.getElementById('siresults').innerHTML = '<strong>Here is your results.</strong><br>'+sirespon[siresult];
document.getElementById('siresults').style.display = '';
return false;
}


Will that is it, your done with the installation way to go.  ;)

Now when you go to post you need to have the HTML BBC available to you, usually Admins.

Here is how you would post.
[html]
<form onsubmit="return sisurvey(this,'Your red,Your green,Your blue')">
<br />Do you like the sunset?<br />
<input type="radio" name="1" value="0"> Yes<br />
<input type="radio" name="1" value="1"> No<br />
<input type="radio" name="1" value="0"> Sometimes<br />
<br />Do you like the ocean?<br />
<input type="radio" name="2" value="2"> Yes<br />
<input type="radio" name="2" value="0"> No<br />
<input type="radio" name="2" value="2"> Maybe<br />
<br />Do you like the outdoors<br />
<input type="radio" name="3" value="1"> Yes<br />
<input type="radio" name="3" value="2"> No<br />
<input type="radio" name="3" value="0"> What outdoors<br />
<br />What is your favorite season<br />
<input type="radio" name="4" value="2"> Winter<br>
<input type="radio" name="4" value="1"> Spring<br />
<input type="radio" name="4" value="0"> Summer<br>
<input type="radio" name="4" value="0"> Fall<br />
<input type="submit" value="Submit" />
</form>
<div id="siresults" style="display:none;"></div>
[/html]


Now a little explaining.
<form onsubmit="return sisurvey(this,'Your red,Your green,Your blue')">
This line here has your responses or outcomes separated by a comma. Here we have 3 outcomes Your red, Your green, Your blue.

<input type="radio" name="1" value="0"> Yes<br />
<input type="radio" name="1" value="1"> No<br />
<input type="radio" name="1" value="0"> Sometimes<br />

These are the form lines, the names really don't matter but be sure to keep them the same within a question.

The part that matters is the value. Here a value of 0 equals red and the value of 1 equals green. The values are in order as the outcomes in the previous code snippet. So Your red would be 0, Your green would be 1 and Your blue would be 3. In the script these values are counted and return the outcome.

After that you do your next question with a different name, and continue until your done.

For a live example you can take the survey using the form code above.
http://www.sicomm.us/t/124/
Title: Re: [TIP/TRICK] Surveys, Like what ___ are you.
Post by: Siirist on July 07, 2016, 12:28:05 PM
Quote from: nend on April 09, 2016, 06:49:37 PM
Well I was thinking of writing a mod, but then again I wanted the surveys in my topics.

For a live example you can take the survey using the form code above.
http://www.sicomm.us/contests/what-color-are-you/

Got this
QuoteYou requested the URL www.sicomm.us/contests/what-color-are-you/ but it cannot be found on www.sicomm.us.

Hi nend,

I am very curious as how this will look when compared to a Poll.
Hoping that you can get the sample functioning again.

Be Well,
Siirist
Title: Re: [TIP/TRICK] Surveys, Like what ___ are you.
Post by: d3vcho on July 07, 2016, 01:34:49 PM
Quote from: Siirist on July 07, 2016, 12:28:05 PM
Quote from: nend on April 09, 2016, 06:49:37 PM
Well I was thinking of writing a mod, but then again I wanted the surveys in my topics.

For a live example you can take the survey using the form code above.
http://www.sicomm.us/contests/what-color-are-you/ (http://www.sicomm.us/contests/what-color-are-you/)

Got this
QuoteYou requested the URL www.sicomm.us/contests/what-color-are-you/ (http://www.sicomm.us/contests/what-color-are-you/) but it cannot be found on www.sicomm.us (http://www.sicomm.us).

Hi nend,

I am very curious as how this will look when compared to a Poll.
Hoping that you can get the sample functioning again.

Be Well,
Siirist


Hey Siirist, check this link: http://www.sicomm.us/t/124/

Good work Nend, I like it. It's very simple but also very functional  :laugh:
Title: Re: [TIP/TRICK] Surveys, Like what ___ are you.
Post by: Siirist on July 07, 2016, 09:57:08 PM
Works well and as intended.   :)

Although I am not interested at this time, I know where to come back to.   8)

Good job.   ;)

Be Well,
Siirist