Jump to content

Recommended Posts

...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!!!

Link to comment
https://forums.phpfreaks.com/topic/168350-solved-easy-radio-button-question/
Share on other sites

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....

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.