Advertisement:

Author Topic: [TIP/TRICK] Surveys, Like what ___ are you.  (Read 4353 times)

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
[TIP/TRICK] Surveys, Like what ___ are you.
« 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.
Code: [Select]
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.
Code: [Select]
[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.
Code: [Select]
<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.

Code: [Select]
<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/
« Last Edit: August 01, 2016, 12:20:58 AM by nend »

Offline Siirist

  • Sophist Member
  • *****
  • Posts: 1,201
  • Gender: Male
  • What would Kindred or Illori do?
    • A Sanctuary (Support for Survivors of abuse/Info for Family & Friends)
Re: [TIP/TRICK] Surveys, Like what ___ are you.
« Reply #1 on: July 07, 2016, 12:28:05 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
Quote
You 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
Needed information:
- What's your SMF version?, What MODs are installed? What Theme(s) are you using? Have you backed up your database?

--> At least 50% of the questions asked are answered in the Online Manual <--
Mods for ver 1.x will NOT run on ver 2.x

Offline d3vcho(void)

  • On Hiatus
  • SMF Hero
  • *
  • Posts: 2,246
  • Gender: Male
  • Be prepared for hell
    • d3vcho on GitHub
    • @SMF_ES on Twitter
Re: [TIP/TRICK] Surveys, Like what ___ are you.
« Reply #2 on: July 07, 2016, 01:34:49 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
Quote
You 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


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:

Offline Siirist

  • Sophist Member
  • *****
  • Posts: 1,201
  • Gender: Male
  • What would Kindred or Illori do?
    • A Sanctuary (Support for Survivors of abuse/Info for Family & Friends)
Re: [TIP/TRICK] Surveys, Like what ___ are you.
« Reply #3 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
Needed information:
- What's your SMF version?, What MODs are installed? What Theme(s) are you using? Have you backed up your database?

--> At least 50% of the questions asked are answered in the Online Manual <--
Mods for ver 1.x will NOT run on ver 2.x