woodplease Posted February 1, 2010 Share Posted February 1, 2010 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 Link to comment https://forums.phpfreaks.com/topic/190553-calculating-total-from-radio-button-values/ Share on other sites More sharing options...
woodplease Posted February 1, 2010 Author Share Posted February 1, 2010 Also, is it possible to use php to calculate the total value of the selected radio buttons?, instead of using javascript, as i want to use the result to read from a database Link to comment https://forums.phpfreaks.com/topic/190553-calculating-total-from-radio-button-values/#findComment-1005058 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.