Jump to content

calculating total from radio button values


woodplease

Recommended Posts

i'm using php to display radion buttons, each with a different value(values taken from a database), and then i'm using javascript to calculate these values, and the displaying the results in a text box, except it is not calculating or displaying the result. Could anybody help with this?

the php that creates the radio buttons

 
$result3 = pg_query($query2) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows2 = pg_num_rows($result3);

for($j = 0; $j < $numofrows2; $j++) {
	$row2 = pg_fetch_array($result3); //get a row from our result set

	echo "
	<p>Answer ".$row2['answerno']." : <input type=\"radio\" name=\"" . $row['questionno'] . "\" id=\"" .$row2['answerref'] ."\" value=\"" . $row2['answervalue'] . "\"  >".$row2['answer']."</p>

	\n";

}

}
echo'
<input type="button" name="process" onclick="UpdateCost()" value="Calculate Total"/>
<input type="text" id="total" value="0.00" style="background-color:#999999"/>';
?>

 

the javascript that is supposed to calculate the total

function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=0; i<10; i++) {
    gn = 'answerref'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('total').value = sum.toFixed(2);
} 


 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.