newphpcoder Posted April 12, 2012 Share Posted April 12, 2012 Hi.. I have checkbox and I need to get his value. I create function for checkbox if checkbox is clicked it is true then the checkbox value is H else W. function chk(){ if(document.getElementById('H').checked==true){ var H = document.getElementById('H'). value = H; } else if(document.getElementById('H').checked==false){ var H = document.getElementById('H'). value = W; } } and I have function for save : function ApproveLeaveOP(){ var H = document.getElementById('H').value; document.sampleform.action="AddLeave.php?H="+H; document.sampleform.submit(); } <input type="checkbox" name="H" id="H" value="" onclick="chk()"> I don't know how can I add value to the checkbox if I click the value will be H if not the value is W. I am new in using checkbox. Thank you so much.. Quote Link to comment https://forums.phpfreaks.com/topic/260777-save-undefined-value-from-checkbox/ Share on other sites More sharing options...
kicken Posted April 12, 2012 Share Posted April 12, 2012 If you need two mutually exclusive values you should be using two radio buttons or a select element, not a checkbox. Checkboxes have one value which is either active or not depending on the checked state. Using JS to change it like your attempting to do is a horribly wrong usage or checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/260777-save-undefined-value-from-checkbox/#findComment-1336586 Share on other sites More sharing options...
newphpcoder Posted April 12, 2012 Author Share Posted April 12, 2012 can you give me sample using radio button? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/260777-save-undefined-value-from-checkbox/#findComment-1336595 Share on other sites More sharing options...
kicken Posted April 12, 2012 Share Posted April 12, 2012 <input type="radio" name="approveLeave" value="H"> <input type="radio" name="approveLeave" value="W"> That will make two radio buttons on the page, and the user will only be able to select one or the other. When you submit them in your server-side script they will be under the field name approveLeave with a value of either H (if the first is picked) or W (if the second). Quote Link to comment https://forums.phpfreaks.com/topic/260777-save-undefined-value-from-checkbox/#findComment-1336596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.