Advanced Search

Results 1 to 4 of 4

Thread: survey adding script

  1. #1
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default survey adding script

    Hi everybody,

    i'm new to all this and hoping you could help me out on something. i'm looking to setup a survey with 55 questions, each of them with a slot to fill in a ratingnumber from 0 to 4. when all rating numbers are filled in, the user should be able to see the total (all rating numbers added).

    Does anybody know where i can find a script like that or who could helpwith this simple script?

    thank you, Ed

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In Javascript? Certainly.
    Code:
    <form action="" method="">
      <table style="border-style:none;border-width:0;">
        <tr>
          <th>0</th>
          <th>1</th>
          <th>2</th>
          <th>3</th>
          <th>4</th>
        </tr>
        <tr>
          <th>Question One:</th>
          <td><input type="radio" value="0" name="q1" /></td>
          <td><input type="radio" value="1" name="q1" /></td>
          <td><input type="radio" value="2" name="q1" /></td>
          <td><input type="radio" value="3" name="q1" /></td>
          <td><input type="radio" value="4" name="q1" /></td>
        </tr>
        <tr>
          <th>Question Two:</th>
          <td><input type="radio" value="0" name="q2" /></td>
          <td><input type="radio" value="1" name="q2" /></td>
          <td><input type="radio" value="2" name="q2" /></td>
          <td><input type="radio" value="3" name="q2" /></td>
          <td><input type="radio" value="4" name="q2" /></td>
        </tr>
        <!-- And so on... -->
      </table>
      <p id="total" style="height: 1em;"></p>
      <input type="button" onclick="
        var e = this.form.elements, i, j, total = 0;
        for(j=1;e['q' + j];j++)
          total += parseInt(e['q' + j].value);
        document.getElementById('total').innerHTML = total;
      " />
    </form>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey,

    thank you so much for your script, i'll be using it for sure...

    i found a glitch when testing it in dreamwaever, tried to solve it myslef but don't find my way around it.

    the zero score appears above the questions instead of above the radio buttons, and also when i select for example 2 and for the next question 3, pressing the button on the bottom shows "NaN" instead of the result of 5

    Ed

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're right, my fault. Corrected code below.
    Code:
    <html>
    	<head>
    		<title>Hello</title>
    	</head>
    	<body>
    		<form action="" method="">
    			<table style="border-style:none;border-width:0;">
    				<tr>
    					<td>&nbsp; </td>
    
    					<th>0</th>
    					<th>1</th>
    					<th>2</th>
    					<th>3</th>
    					<th>4</th>
    				</tr>
    
    				<tr>
    					<th>Question One:</th>
    					<td><input type="radio" value="0" name="q1" /></td>
    					<td><input type="radio" value="1" name="q1" /></td>
    					<td><input type="radio" value="2" name="q1" /></td>
    					<td><input type="radio" value="3" name="q1" /></td>
    					<td><input type="radio" value="4" name="q1" /></td>
    				</tr>
    
    				<tr>
    					<th>Question Two:</th>
    					<td><input type="radio" value="0" name="q2" /></td>
    					<td><input type="radio" value="1" name="q2" /></td>
    					<td><input type="radio" value="2" name="q2" /></td>
    					<td><input type="radio" value="3" name="q2" /></td>
    					<td><input type="radio" value="4" name="q2" /></td>
    				</tr>
    
    				<!-- And so on... -->
    			</table>
    			<p id="total" style="height: 1em;"></p>
    			<input type="button" onclick="
    				var e = this.form.elements, j, k, total = 0;
    				for(j=1;e['q' + j];j++)
    					for(k=0;e['q' + j][k];k++)
    						if(e['q' + j][k].checked) {
    							if(!isNaN(parseInt(e['q' + j][k].value))) total += parseInt(e['q' + j][k].value);
    							break;
    						}
    				document.getElementById('total').innerHTML = total;
    			" />
    		</form>
    	</body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •