Link to home
Start Free TrialLog in
Avatar of wobbled
wobbledFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Javascript radio buttons - save values to variables

I have a simple form that has a various groups of radio buttons.  I have some Javascript code that when you click the submit button runs.  This code should loop through each group of buttons and then save that value to the variable.

The problem is that it seems to only run on the first group and then jumps out of the loop.  I have also written out the same code for each radio button rather than using a function to try to see why it is failing but can not work this out.

Once the Javascript variable holds a value I wish to then assign that variable to an ASP variable, which I believe you can easily do??

Could someone please have a look at my code and make the necessary changes.
<body>
 
	
	<script language="JavaScript" type="text/javascript">
 
function whichButton() {
var Ufishingquality
var Upegs
var Uaccess
var Uclean
var Ufacilities
var er_msg
var i	
	alert("started")
	
	for (i=0; i<document.readersreview.fishquality.length; i++) {
	
		if (document.readersreview.fishquality[i].checked) {
		Ufishingquality = document.readersreview.fishquality[i].value
		}		
	}
	
	//alert (Ufishingquality)
	
	for (i=0; i<document.readersreview.pegs.length; i++) {
	
		if (document.readersreview.pegs[i].checked) {
		Upegs = document.readersreview.pegs[i].value
		}		
	}
	
	//alert (Upegs)
	
	for (i=0; i<document.readersreview.access.length; i++) {
	
		if (document.readersreview.access[i].checked) {
		Uaccess = document.readersreview.access[i].value
		}		
	}
	
	for (i=0; i<document.readersreview.clean.length; i++) {
	
		if (document.readersreview.clean[i].checked) {
		Uclean = document.readersreview.clean[i].value
		}		
	}
	
	for (i=0; i<document.readersreview.fishquality.length; i++) {
	
		if (document.readersreview.facilities[i].checked) {
		Ufacilities = document.readersreview.facilities[i].value
		}		
	}
	
	// below just my test outputs etc
	alert ("END")
	if(Ufishingquality){ //if(firstgroup) is just saying, "if firstgroup does not equal null"
	alert("I like " +  fishingquality + " best.")
	}
	else{
	er_msg = er_msg + "Fishing Quality "
	alert(er_msg)
	}
}
 
	</script>
 
			<form action="/reader_review.asp?restid=24440" method="post" name="readersreview" id="readersreview">
	<table width="900" cellpadding="0" cellspacing="0" align="center">  
		<tr>
			<td>
			<div class="userVote">Title for your reply</div>	
			<div class="userVote"><input name="readerstitle" type="text" class="usertextfield" id="readersname" value="" /></div>
			</td>
		</tr>
		<tr>
			<td>
			<div class="reviewleft">Review</div>
			<div class="reviewright">
			  <p><textarea name="review" class="reviewlarge" id="review" /></textarea> </p>
			</div>
			</td>
		</tr>			
	</table>
	<br />
	<br />		
	<div class="votingpanel">
      <table width="900" align="center" cellpadding="0" cellspacing="0" class="votingtable">
            <tr>
              <td width="100"><img src="/images/spacer.gif" alt="vote now" width="118" height="1" /></td>              
              <td class="votingtext">1</td>
              <td class="votingtext">2</td>
              <td class="votingtext">3</td>
              <td class="votingtext">4</td>
              <td class="votingtext">5</td>
              <td class="votingtext">6</td>
              <td class="votingtext">7</td>
              <td class="votingtext">8</td>
              <td class="votingtext">9</td>
              <td class="votingtext">10</td>
            </tr>
            <tr>
              <td>&nbsp;&nbsp;Quality of fishing</td>         
			  <td class="votingtext"><input name="fishquality" type="radio" value="0" /></td>     
              <td class="votingtext"><input name="fishquality" type="radio" value="1" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="2" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="3" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="4" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="5" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="6" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="7" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="8" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="9" /></td>
              <td class="votingtext"><input name="fishquality" type="radio" value="10" /></td>
            </tr>
            <tr>
              <td>&nbsp;&nbsp;Pegs</td>          
			  <td class="votingtext"><input name="pegs" type="radio" value="0" /></td>   
              <td class="votingtext"><input name="pegs" type="radio" value="1" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="2" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="3" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="4" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="5" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="6" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="7" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="8" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="9" /></td>
              <td class="votingtext"><input name="pegs" type="radio" value="10" /></td>
            </tr>
            <tr>
              <td>&nbsp;&nbsp;Access to venue</td>     
			  <td class="votingtext"><input name="access" type="radio" value="0" /></td>         
              <td class="votingtext"><input name="access" type="radio" value="1" /></td>
              <td class="votingtext"><input name="access" type="radio" value="2" /></td>
              <td class="votingtext"><input name="access" type="radio" value="3" /></td>
              <td class="votingtext"><input name="access" type="radio" value="4" /></td>
              <td class="votingtext"><input name="access" type="radio" value="5" /></td>
              <td class="votingtext"><input name="access" type="radio" value="6" /></td>
              <td class="votingtext"><input name="access" type="radio" value="7" /></td>
              <td class="votingtext"><input name="access" type="radio" value="8" /></td>
              <td class="votingtext"><input name="access" type="radio" value="9" /></td>
              <td class="votingtext"><input name="access" type="radio" value="10" /></td>
            </tr>
            <tr>
              <td>&nbsp;&nbsp;General Cleaniness of Venue</td>
              <td class="votingtext"><input name="clean" type="radio" value="0" /></td>
              <td class="votingtext"><input name="clean" type="radio" value="1" /></td>
              <td class="votingtext"><input name="clean" type="radio" value="2" /></td>
              <td class="votingtext"><input name="clean" type="radio" value="3" /></td>
              <td class="votingtext"><input name="clean" type="radio" value="4" /></td>
              <td class="votingtext"><input name="clean" type="radio" value="5" /></td>              
            </tr>
			<tr>
              <td>&nbsp;&nbsp;Facilities</td>
              <td class="votingtext"><input name="facilities" type="radio" value="0" /></td>
              <td class="votingtext"><input name="facilities" type="radio" value="1" /></td>
              <td class="votingtext"><input name="facilities" type="radio" value="2" /></td>
              <td class="votingtext"><input name="facilities" type="radio" value="3" /></td>
              <td class="votingtext"><input name="facilities" type="radio" value="4" /></td>
              <td class="votingtext"><input name="facilities" type="radio" value="5" /></td>              
            </tr>
      </table>
    </div>
 
<input name="useruid" type="hidden" value="<%session("username")%>" />
<input name="cerestuid" type="hidden" value="24440" />
<input name="Submit" type="submit" class="orangebutton" value="Send Review" onClick="whichButton()"/></div>
</form>
 
 
</body>

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

>>Once the Javascript variable holds a value I wish to then assign that variable to an ASP variable, which I believe you can easily do??

You need to do a form posting, so that you can save the selected value into ASP (Session) variable.
Avatar of wobbled

ASKER

Thanks for the reply on that, I usually wrap my ASP into the same page so I'll set the JS variable to write into the ASP variable at that point.

My main problem is getting the JS Variable to accept the value after the first For loop.  seems to jump out of the bit of script after one group of radio buttons
ASKER CERTIFIED SOLUTION
Avatar of wobbled
wobbled
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial