Hello,

I am having problems retrieving correct values from two radio buttons in my AJAX script below.

The radio button values are either a 1 or 0 and always produces 0 regardless of which button is selected.

Once in AJAX, I can not have the script retreive a 1 or 0. AJAX script produces "[object HTMLInputElement]" as the response. Not sure what is going wrong.

Thanks in Advance.
Diego

Help with Code Tags

<script type="text/javascript"> 

var settings = getXmlHttpRequestObject(); function saveCar() { 
if (settings.readyState == 4 || settings.readyState == 0) { 

var car = escape(document.getElementById('car').value);

/* PROBLEM - Radio Button Processor */
var proc17 = document.getElementById("search17");
for (var i = 0; i < proc17.length; i++)
{
    if (proc17[i].checked){
       search17 = proc17[i].value;
       break;
    }
}


settings.open("GET", 'abc.php?proc17=' + proc17 + '&car=' +car, true); 
settings.onreadystatechange = handleSettings; settings.send(null);}}
function handleSettings() { if (settings.readyState == 4) { 

		}
	}
</script>

How many radio buttons' do you have in your form?

I am using only two radio buttons. 1 for yes and 0 for no.

This was just an example u wil have 2 replace all the value to the specific name of your radio buttons

var radioNames = [ 'radio1', 'search17' ];
for (var i = 0; i <= 1; i++) 
{ 
if (document.getElementById(radioNames).checked )
{ search17 = proc17.value; } 
}

but it would be nice if u can provide the exact name value of the two radio buttons.

Sorry if am bein hasty on that 1! Lets assume that you have 2 radio buttons with different name values!

var radioNames = [ 'radio1', 'search17' ]; for (var i = 0; i <= 1; i++) {
var proc17 = document.getElementById(radioNames[i]); 
 if ( proc17.checked ) { search17 = proc17.value; } 
}

Another option!

<script type="text/javascript">

function saveCar( e )
{ e = e ? e : window.event;
  t = e.target ? e.target : e.srcElement;
// Things to do -->

if (( t.id ) && ( t.id == 'search17' ) && ( t.checked )) 
{ search17 = t.value; }

}  
</script>
Member Avatar for langsor

I would like to help, but I guess I just don't understand what the real problem is ... my quickie write-up of you above code delivers both 1 and 0

What am I missing here?

<html>
<head>
<script type="text/javascript"> 
function i_am_ajax () {
  var form = document.getElementById("my_form");
  for ( var i = 0; i < form.length; i++ ) {
    if ( form[i].checked ){
       alert( form[i].value );
       break;
    }
  }
};
</script>
</head>
<body>
<form id="my_form">
  <label><input type="radio" name="radio" value="0" />NO</label><br />
  <label><input type="radio" name="radio" value="1" />YES</label><br />
  <input type="button" onclick="i_am_ajax()" value="response" />
</form>
</body>
</html>

Updates!

<html>
<head>
<title></title>
<script type="text/javascript">
<!--
document.onclick = thisValue; function thisValue( e ) 
{ e = e ? e : window.event;
  f = e.target ? e.target : srcElement;

form1.txt1.value = f.value;
f = f.name && f.name == 'r1' || f.name == 'b1' ? alert(f.value) : f; 
}

//-->
</script>
</head>
<form name="form1" action="#" onsubmit="return false;">
<label for="id0">
<input type="radio" value="sampleValue1" id="id0" name="r1" />&nbsp;Radio 1</label>
<label for="id1">
<input type="radio" value="sampleValue2" id="id1" name="r1" />&nbsp;Radio 2</label><br /><br />
<input type="text" id="id2" name="txt1" />
<input type="button" id="id3" name="b1" value="accept" accesskey="3" />

</form>
 
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.