0

How to count the total value of radio button within the same page and pass to another php file? The total will be count at this page and i can get the total from answer.php file.

<form  action="answer.php" method="POST">
<input type="radio" name="q1" value="1" />Yes <br />
<input type="radio" name="q1" value="0" />No <br />
<input type="radio" name="q2" value="2" />Yes <br />
<input type="radio" name="q2" value="0" />No <br />
<input type="radio" name="q3" value="3" />Yes <br />
<input type="radio" name="q3" value="0" />No <br />
<input type="submit"  value="submit" name="submit"/>
</form>
Darcy
  • 63
  • 5
  • 13

3 Answers3

2

I suggest using an array to count your values.

<input type="radio" name="q[]" value="2" />
<input type="radio" name="q[]" value="3" />
<input type="radio" name="q[]" value="4" />
<input type="radio" name="q[]" value="5" />

This will result in $_POST['q'] being an array. You can now do:

echo "The total amount is ".array_sum($_POST['q']);
Peter
  • 8,776
  • 6
  • 62
  • 95
1

Using jQuery- it is easy, just iterate through the inputs and tally up the values. Note that I gave the form an Id so it can be targetted directly if you have other form. The total can be passed to your other page - either via AJAX or using a standard HTML form as a hidden field. Alternatively - since this is a form and you are already passing it to a PHP page - you could simply submit the form and tally up the $_POST variables on the other side.

$('#testForm input').on('change', function() {
   var total=0;
  $('input[type=radio]:checked', '#testForm').each(function(){
    total += parseInt($(this).val());
    })
  alert(total)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form  id="testForm" action="answer.php" method="POST">
<input type="radio" name="q1" value="1" />Yes <br />
<input type="radio" name="q1" value="0" />No <br />
<input type="radio" name="q2" value="2" />Yes <br />
<input type="radio" name="q2" value="0" />No <br />
<input type="radio" name="q3" value="3" />Yes <br />
<input type="radio" name="q3" value="0" />No <br />
<input type="submit"  value="submit" name="submit"/>
</form>

Commented version for the OP:

$('#testForm input').on('change', function() {//triggers the function on any change in the form
       var total=0;//initialises the total at 0 so that each round ottallying up resets before the tally
      $('input[type=radio]:checked', '#testForm').each(function(){//selects each input in the #testForm that is checked
        total += parseInt($(this).val());//adds the value of each checked radio button to the tally
        })
      alert(total); //alerts the final tally after all iterations
    });
gavgrif
  • 15,194
  • 2
  • 25
  • 27
1

You don't need jquery for this. Add a class to your radio buttons so we can query them without risking getting other elements in the page, something like "my-radio"

This javascript will get you the sum:

function getRadioButtonsSum(radioClass) {
  var radioBtns = document.querySelectorAll(radioClass);

  var count = 0;
  var i;

  for (i = 0; i < radioBtns.length; i += 1) {
    if (radioBtns[i].checked) {
      count += +radioBtns[i].value;
    }
  }
  return count;
}

getRadioButtonsSum('.my-radio');
  • @gavgrif No, because the value is a string, it has to be casted to int otherwise count will be a concatenation of all the strings ("010203" instead of 6). – Salomao Rodrigues May 08 '16 at 21:59
  • oh - ok - I used parseInt() for that. I didn't know about putting a plus in front to achieve the same effect. Thanks – gavgrif May 08 '16 at 22:02
  • You're welcome :) Maybe I should use parseInt for readability, although they produce different results: http://stackoverflow.com/questions/17106681/parseint-vs-unary-plus-when-to-use-which – Salomao Rodrigues May 08 '16 at 22:05