DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Radio Button - AJAX - Problem with Passing Value (http://www.daniweb.com/forums/thread144236.html)

lonestar23 Sep 5th, 2008 4:34 pm
Radio Button - AJAX - Problem with Passing Value
 
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>

essential Sep 5th, 2008 4:55 pm
Re: Radio Button - AJAX - Problem with Passing Value
 
How many radio buttons' do you have in your form?

lonestar23 Sep 5th, 2008 5:07 pm
Re: Radio Button - AJAX - Problem with Passing Value
 
I am using only two radio buttons. 1 for yes and 0 for no.

essential Sep 5th, 2008 5:58 pm
Re: Radio Button - AJAX - Problem with Passing Value
 
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.

essential Sep 5th, 2008 9:48 pm
Re: Radio Button - AJAX - Problem with Passing Value
 
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; }
}

essential Sep 5th, 2008 11:45 pm
Re: Radio Button - AJAX - Problem with Passing 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>

langsor Sep 6th, 2008 2:51 am
Re: Radio Button - AJAX - Problem with Passing Value
 
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>

essential Sep 6th, 2008 5:09 am
Re: Radio Button - AJAX - Problem with Passing Value
 
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>


All times are GMT -4. The time now is 8:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC