rahul_patel Posted August 1, 2009 Share Posted August 1, 2009 ...but the answer is driving me crazy. This 'should' be one of the easiest things to do but everywhere I look for examples or tutorials they never work. My form... <script src="myscript.js" language="javascript"></script> <form action="javascript:insert()" method="post"> <input name="gender" type="radio" id="male" value="Male" />M <br> <input name="gender" type="radio" id="female" value="Female" />F<br> <input type="submit" name="Submit" value="Insert"/> </form> <div id="insert_response"></div> myscipt.js function insert() { document.getElementById('insert_response').innerHTML = "Just a second..." var gender = document.getElementByName('gender').value; nocache = Math.random(); http.open('get', 'insert.php?gender='+gender+'&nocache = '+nocache); http.onreadystatechange = insertReply; http.send(null); } function insertReply() { if(http.readyState == 4){ var response = http.responseText; document.getElementById('insert_response').innerHTML = 'Site added:'+response; } } insert.php <?php $gender = $_GET['gender']; echo $gender; ?> All pretty standard code right? So why do I ALWAYS get the answer male whether I select female or male? And to be honest I don't know whether it's a php issue or a javascript issue but I do know it's an issue. There's a nice fresh blueberry muffin for the person that can help!!! Quote Link to comment https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/ Share on other sites More sharing options...
fooDigi Posted August 1, 2009 Share Posted August 1, 2009 is a javascript question... but will try and help anyway, since i don't have the power to move. try this to get your gender value from the radio buttons... var gender_radio = document.getElementsByName('gender'); for(var x=0;x<gender_radio.length;x++) if(gender_radio[x].checked) gender = gender_radio[x].value; hope this works for you.... Quote Link to comment https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/#findComment-888069 Share on other sites More sharing options...
rahul_patel Posted August 1, 2009 Author Share Posted August 1, 2009 hope this works for you.... Thanks for that. As with most times I post looking for help on a forum I somehow stumble upon the answer through sheer luck. Or maybe it's actually taking time away from the problem, I don't know. I feel though it was a bit of both, PHP and javascript but wherever this topic ends up, here is what got it working. <input name="gender" type="radio" id="Male" value="Male" />Male <input name="gender" type="radio" id="Female" value="Female" />Female <input name="gender" type="radio" id="Iffy" value="iffy" />Iffy if(document.getElementById('Male').checked==true){ var gender = 'Male'; }else if (document.getElementById('Female').checked==true) { var gender = 'Female'; } else { var gender = 'Iffy'; } I appreciate your time in helping and hope the code will be useful to others with the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/#findComment-888082 Share on other sites More sharing options...
fooDigi Posted August 1, 2009 Share Posted August 1, 2009 cool, glad you got it working... but just to point out something that may help in the future... if you have many (say 100) radio elements named the same, your solution would require still an elaborate if/elseif structure to get it to work... and if all you want is the value of the one checked, loop through them and return the selected value (ie the solution above)... just fyi, may make your life easier in the long run... can't believe a javascript topic stayed in the php forum until it was solved Quote Link to comment https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/#findComment-888096 Share on other sites More sharing options...
rahul_patel Posted August 1, 2009 Author Share Posted August 1, 2009 may make your life easier in the long run... Duly noted. I'm getting it all connected to the db and working, then I'll experiment with the code you provided. Luckily I only have a few radio buttons . Quote Link to comment https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/#findComment-888115 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.