PHP pass total value of radio button - javascript
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>
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']);
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
});
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');
Related
Set hidden value based on radio button selection
Cannot find an answer: I have searched Stack Overflow, however, despite finding lots of similar posts — and more complicated situations — I still couldn't find an answer to the issue I am trying to solve. Here's my issue: I have four radio buttons, and one hidden field: <!-- My HTML Document --> <form action="/my-doc.html" method="post"> <!-- The 4 Radio Buttons--> <input type="radio" name="game" value="1" checked> First <input type="radio" name="game" value="2"> Second <input type="radio" name="game" value="3"> Third <input type="radio" name="game" value="4"> Fourth <!-- The Hidden Field --> <input type="hidden" name="criteria" value="1"> <!-- My Submit Button --> <input type="submit" name="action" value="Go"> </form> What I need to do is set the value of <input type="hidden" name="criteria" value="1"> so that it is 0 Like this: <input type="hidden" name="criteria" value="0"> ...but only after the user selects either the first, or second, radio button. The value of the hidden field should remain as being equal to 1 if any other radio button is selected. How does a person do this using JavaScript? Requirements: "Looking for a VanillaJS answer."
you can try below option In javascript function setValue() { var selectedRadio = ''; var games = document.getElementsByName('game') for (var i = 0; i < games.length; i++) { if (games[i].checked) { selectedRadio = games[i].value; } } document.getElementById("hdnSelectedRadValue").value = (selectedRadio == "1" || selectedRadio == "2") ? "0" : "1"; return false; } Changes to do in HTML side <body style="background-color: #f2f2f2;"> <form action="some.htm" method="post"> <input type="radio" name="game" value="1" checked> First <input type="radio" name="game" value="2"> Second <input type="radio" name="game" value="3"> Third <input type="radio" name="game" value="4"> Fourth <input type="text" name="criteria" id="hdnSelectedRadValue"> <input type="submit" name="action" value="Go" onclick="setValue();"> </form> </body>
var radios = document.querySelectorAll('input[type=radio][name="game"]'); radios.forEach(radio => radio.addEventListener( 'change', () => { document.getElementsByName("criteria")[0].value = parseInt(radio.value, 10) > 2 ? '1' : '0'; } )); <input type="radio" name="game" value="1" checked> First <input type="radio" name="game" value="2"> Second <input type="radio" name="game" value="3"> Third <input type="radio" name="game" value="4"> Fourth <input type="hidden" name="criteria" value="1"> <input type="submit" name="action" value="Go">
You can simply listen to "change" events on all of the radio buttons, then just set the value accordingly. Here's the snippet code I have written and tested (function(){ let hdfValue = document.getElementById("myhiddenfield") let radioButtons = document.querySelectorAll('input[name="game"]'); let submitButton = document.querySelector('input[name="action"]') radioButtons.forEach((input) => { input.addEventListener('change', function(e){ let radioButtonValue = e.target.value if(radioButtonValue == 1 || radioButtonValue == 2){ hdfValue.value = 0; } else { hdfValue.value = 1; } }); }); submitButton.addEventListener("click", function() { console.log(hdfValue.value) }); })() <form> <input type="radio" name="game" value="1" checked> First <input type="radio" name="game" value="2"> Second <input type="radio" name="game" value="3"> Third <input type="radio" name="game" value="4"> Fourth <input type="hidden" name="criteria" id="myhiddenfield" value="1"> <input type="button" name="action" value="Go"> </form>
Checking if a particular checkbox in a checkbox list is checked
I have a list of checkboxes: <input name="choice" type="checkbox" id="choice1" value="A" /> <input name="choice" type="checkbox" id="choice2" value="B" /> <input name="choice" type="checkbox" id="choice3" value="C" /> <input name="choice" type="checkbox" id="choice4" value="D" /> Name is the same for all but id is different. I need to check if a particular checkbox (for example the one with id=choice3 is checked. Tried if (this.choice.id === "choice3" && this.choice[2].checked) { alert("checked!"); } but it does not work - the alert is never reached P.S. I need to use javascript not jquery
Thats how you do it without jQuery: Suppose your form is like this: <form id="myForm" action="test.php"> <input name="choice" type="checkbox" id="choice1" value="A"/> <input name="choice" type="checkbox" id="choice2" value="B"/> <input name="choice" type="checkbox" id="choice3" value="C"/> <input name="choice" type="checkbox" id="choice4" value="D"/> <input type="button" onclick="validate();" value="Submit form"> </form> You can do the validation on submit this way: function validate() { if (document.getElementById('choice3').checked) { alert("checked"); } else { alert("You didn't check it! "); } }
how do I select a parameter a Javascript selector?
Hi there (I'm a complete JS newbie so please no bullying) I'm trying to pass formID inside my score variable but I don't think I'm getting my selector right, can anyone tell me what I'm doing wrong ? Edit: Sorry everyone, I'm getting tired. My issue: I cannot properly select the value of my score var. JS function addScore(formId) { var show_id = $('#show_id-'+formId).val(); var user_id = $('#user_id-'+formId).val(); var score = $('input[value="tvshowrating' + formID +'"]').val(); HTML <form id="40"> <div class="your-score"> <div class="">Your Score</div> <div id="flash"></div> <input class="hover-star" type="radio" name="tvshowrating40" value="1" title="1" /> <input class="hover-star" type="radio" name="tvshowrating40" value="2" title="2" /> <input class="hover-star" type="radio" name="tvshowrating40" value="3" title="3" /> <input class="hover-star" type="radio" name="tvshowrating40" value="4" title="4" /> <input class="hover-star" type="radio" name="tvshowrating40" value="5" title="5" /> <input class="hover-star" type="radio" name="tvshowrating40" value="6" title="6" /> <input class="hover-star" type="radio" name="tvshowrating40" value="7" title="7" /> <input class="hover-star" type="radio" name="tvshowrating40" value="8" title="8" /> <input class="hover-star" type="radio" name="tvshowrating40" value="9" title="9" /> <input class="hover-star" type="radio" name="tvshowrating40" value="10" title="10" /> <input type="hidden" id="show_id-40" value="40" /> <input type="hidden" id="user_id-40" value="2" /> <span id="hover-test" style="margin:0 0 0 20px;"></span> <input id="submitscore" type="submit" value="Submit scores!" onclick="addScore(40);" /> </div> </form> </div>
You probably want the checked value: $('input[name="tvshowrating' + formID +'"]:checked').val() Here is another question that answers your problem: Get Value of Radio button group or How can I know which radio button is selected via jQuery?
You're selecting <input>s by value. You probably want input[name=....
JavaScript radio buttons
I have a few radio buttons, and when I select one of them, I also have to check another one. For example, if I select yes on a radio button, another radio button must be automatically checked with no. I tried a few scripts but don't seem to work. Does anyone know a solution? I'm new in JS. Thanks in advance!
> Live Demo < <!--HTML--> <input type="radio" name="group_1" value="yes" id="r1">Yes<br> <input type="radio" name="group_1" value="no" id="r2">No<br> <br> <input type="radio" name="group_2" value="yes" id="r3">Yes<br> <input type="radio" name="group_2" value="no" id="r4">No<br> //Script $("input[name='group_1']").click(function(){ if(this.value=="yes"){ $("input#r4").attr("checked",true); }else{ $("input#r3").attr("checked",true); } }); $("input[name='group_2']").click(function(){ if(this.value=="yes"){ $("input#r2").attr("checked",true); }else{ $("input#r1").attr("checked",true); } });
I'm not very certain on what you are trying to achieve but by using the "name" attribute this automatically happens...when you check one radio...the others with the same name get set to unchecked. <input type="radio" name="someoption" value="0" />0 <input type="radio" name="someoption" value="1" />1 <input type="radio" name="someoption" value="2" />2 checking any one of the above will cause the other 2 to be unchecked unless do you may be mean checkboxes or multiple option sets ?
javascript: $('#myradio1').bind('change', function () { $('#myradio3').attr('checked', 'checked'); }); html <input type="radio" name="cols1" value="1" id="myradio1" /> <input type="radio" name="cols1" value="2" id="myradio2" /> <input type="radio" name="cols2" value="1" id="myradio3" /> <input type="radio" name="cols2" value="2" id="myradio4" /> see working example at http://jsfiddle.net/9jXbv/
For HTML markup like below: <div> <input type="radio" name="one" value="yes"> yes <input type="radio" name="one" value="no"> no </div> <div> <input type="radio" name="two" value="yes"> yes <input type="radio" name="two" value="no"> no </div> you can use this JavaScript code: $(":radio").on("change", function() { var that = this; $(":radio").filter(function() { return this.value == "no" && this.name != that.name; }).prop("checked", true); }); DEMO: http://jsfiddle.net/nKLMX/
With jquery: $("#radio1").change(function() { $("#radio2").removeAttr("checked"); }); http://jsfiddle.net/
I've mocked up a pure JS solution to this ( No libraries ) <input type="radio" name="g1" value="1" />Yes <br /> <input type="radio" name="g1" value="0" />No <br /><br /> <input type="radio" name="g2" value="1" />Yes <br /> <input type="radio" name="g2" value="0" />No <script type="text/javascript"> var g1 = document.getElementsByName('g1'); // store g1 elements var g2 = document.getElementsByName('g2'); // store g2 elements // handle click functionality function radio_checked_event(obj, f) { if(obj.addEventListener) { obj.addEventListener('click', f, false); } else if(obj.attachEvent) { obj.attachEvent('onclick', f); } } // when you click on g1 yes radio_checked_event(g1[0], function() { //set g1 no to checked g2[1].setAttribute('checked', 'checked'); }); </script>
Traverse object and return matching key / values
I have a series of objects containing product information that I need to use to filter the available product options. (i.e. Customer selects radio button with value option id 25, I need to filter the other two options based on what's available for id 25 and then get the value for that three part combination from configurations (25 / 1 / 13) and find that combination in siblings to return the sku value (25 / 1/ 13 = 1761). I've been successful at getting the keys and values but not matching. I'm a total n00b with javascript (most of my experience is just animating things with jQuery) so I'm not sure that I have written anything worthwhile so far. Here's what I get from the encoded JSON (trimmed b/c data is massive so formatting might not be exact). I don't have any control over formatting and structure of the JSON. var configurations = { "lens_types":{ "25":{ "1":1, "2":2, "4":4, "3":3}}, "lenses":{ "25":{ "1":{ "2":2, "4":4, "5":5, "6":6, "9":9, "13":13}, "2":{ "4":4, "5":5, "6":6, "13":13}}} var siblings = { "25":{ "1":{ "2":1744, "4":1745, "5":1747}, "6":1749, "9":1752, "13":1761}, "2":{ "4":1746, "5":1748, "6":1750, "13":1762}, "4":{ "1":1753, "3":1756, "8":1759}, "3":{ "2":1754, "3":1755, "8":1757, "9":1758, "12":1760}}} This is the generic structure of the product form: <form method="post" action="" name="product_details"> <div id="frame_style"> <input type="radio" name="frame_style" value="1" /> <input type="radio" name="frame_style" value="3" /> <input type="radio" name="frame_style" value="11" /> <input type="radio" name="frame_style" value="24" /> <input type="radio" name="frame_style" value="25" /> <input type="radio" name="frame_style" value="27" /> <input type="radio" name="frame_style" value="2" /> <input type="radio" name="frame_style" value="8" /> <input type="radio" name="frame_style" value="45" /> <input type="radio" name="frame_style" value="77" /> <input type="radio" name="frame_style" value="89" /> <input type="radio" name="frame_style" value="13" /> </div> <div id="lens_type"> <input type="radio" name="lens_type" value="1" /> <input type="radio" name="lens_type" value="2" /> <input type="radio" name="lens_type" value="3" /> <input type="radio" name="lens_type" value="4" /> </div> <div id="lens_style"> <input type="radio" name="lens_style" value="1" /> <input type="radio" name="lens_style" value="2" /> <input type="radio" name="lens_style" value="3" /> <input type="radio" name="lens_style" value="4" /> <input type="radio" name="lens_style" value="5" /> <input type="radio" name="lens_style" value="8" /> <input type="radio" name="lens_style" value="9" /> <input type="radio" name="lens_style" value="12" /> </div> </form> Any ideas? Thanks!! Edit Here's the full object for both: var configurations = {"lens_types":{"25":{"1":1,"2":2,"4":4,"3":3},"1":{"1":1,"2":2,"4":4,"3":3},"15":{"1":1,"2":2,"3":3},"26":{"1":1,"2":2,"3":3},"24":{"1":1,"2":2,"4":4,"3":3},"27":{"1":1,"2":2,"4":4,"3":3},"11":{"1":1,"2":2,"4":4,"3":3},"3":{"1":1,"2":2,"4":4,"3":3}}, "lenses":{"25":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}},"1":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}},"15":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"3":{"2":2,"3":3,"8":8,"9":9}},"26":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"3":{"2":2,"3":3,"8":8,"9":9}},"24":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}},"27":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}},"11":{"1":{"2":2,"4":4,"5":5,"6":6,"9":9,"13":13},"2":{"4":4,"5":5,"6":6,"13":13},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}},"3":{"1":{"2":2,"4":4,"5":5,"9":9},"2":{"4":4,"5":5,"6":6},"4":{"1":1,"3":3,"8":8},"3":{"2":2,"3":3,"8":8,"9":9,"12":12}}}} var siblings = {"25":{"1":{"2":1744,"4":1745,"5":1747,"6":1749,"9":1752,"13":1761},"2":{"4":1746,"5":1748,"6":1750,"13":1762},"4":{"1":1753,"3":1756,"8":1759},"3":{"2":1754,"3":1755,"8":1757,"9":1758,"12":1760}},"1":{"1":{"2":1769,"4":1770,"5":1772,"6":1774,"9":1777,"13":1786},"2":{"4":1771,"5":1773,"6":1775,"13":1787},"4":{"1":1778,"3":1781,"8":1784},"3":{"2":1779,"3":1780,"8":1782,"9":1783,"12":1785}},"15":{"1":{"2":1794,"4":1795,"5":1797,"6":1799,"9":1802,"13":1807},"2":{"4":1796,"5":1798,"6":1800,"13":1808},"3":{"2":1803,"3":1804,"8":1805,"9":1806}},"26":{"1":{"2":1809,"4":1810,"5":1812,"6":1814,"9":1817,"13":1822},"2":{"4":1811,"5":1813,"6":1815,"13":1823},"3":{"2":1818,"3":1819,"8":1820,"9":1821}},"24":{"1":{"2":1824,"4":1825,"5":1827,"6":1829,"9":1832,"13":1841},"2":{"4":1826,"5":1828,"6":1830,"13":1842},"4":{"1":1833,"3":1836,"8":1839},"3":{"2":1834,"3":1835,"8":1837,"9":1838,"12":1840}},"27":{"1":{"2":1843,"4":1844,"5":1846,"6":1848,"9":1851,"13":1860},"2":{"4":1845,"5":1847,"6":1849,"13":1861},"4":{"1":1852,"3":1855,"8":1858},"3":{"2":1853,"3":1854,"8":1856,"9":1857,"12":1859}},"11":{"1":{"2":1862,"4":1863,"5":1865,"6":1867,"9":1870,"13":1879},"2":{"4":1864,"5":1866,"6":1868,"13":1880},"4":{"1":1871,"3":1874,"8":1877},"3":{"2":1872,"3":1873,"8":1875,"9":1876,"12":1878}},"3":{"1":{"2":1881,"4":1882,"5":1884,"9":1888},"2":{"4":1883,"5":1885,"6":1886},"4":{"1":1889,"3":1892,"8":1895},"3":{"2":1890,"3":1891,"8":1893,"9":1894,"12":1896}}}
You could do something like: $('#frame_style input:radio').click(function(){ var val = $(this).val(); for ( var ind in configurations['lenses'][val]){ var input = '<input type="radio" name="lens_type" value="'+ind+'" >'+ind; $('#lens_type').append(input); } }); $('#lens_type input:radio').live('click', function(){ var frame = $('#frame_style input:radio:checked').val(); var val = $(this).val(); for ( var ind in configurations['lenses'][frame][val]){ var input = '<input type="radio" name="lens_style" value="'+ind+'" >'+ind; $('#lens_style').append(input); } }); $('#lens_style input:radio').live('click', function(){ var frame = $('#frame_style input:radio:checked').val(); var type = $('#lens_type input:radio:checked').val(); var style = $(this).val() alert('Sku is:'+siblings[frame][type][style]); }); the idea is to create the radios after the user select the first level. Of course this is very basic, you can try this fiddle: http://jsfiddle.net/LL3SM/ and choos 25, 1, 2 you will get an alert with the sku