tags:

views:

61

answers:

2

Hi everyone,

do u have an idea of how to send my radio button name for

myAjaxPostrequest.send(parameters);

can parameters be like this:

var answername=document.getElementById('option1').name;

var parameters=answername;

? this code is for using ajax to post a form

and my php page needs the name of the radiobutton clicked

I tried this code and it works as I want except for inserting in the database. It means parameters is the problem

what I want to insert is the number located between brakects of the radiobutton name.

I could do it when I post form without AJAX but now I can't send the name of the radiobutton

any clue about what I can send as parameter for function myAjaxPostrequest.send(parameters); ?

<form id="Question1" method="post"> 
<br />
<P> The sky color is..
</P><img border="0" src="images/wonder.png" width="94" height="134"/><br /><br /><br />
<input type="radio" id="option1" name="answer[1]" value="correct!" onclick="submitFormWithAjax();"/> blue
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> red
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> green
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> white
</form>
A: 

You would use:

var checkedRadio = document.getElementsByName('nameOfRadios');
var isChecked = checkedRadio.checked

So, to get the value of the checked radio do:

var checkedRadio = document.getElementsByName('nameOfRadios');
var isChecked = checkedRadio.checked;
var checkedValue;
for (var i=0; i<checkedRadio.length; i++) {
    if (checkedRadio[i].checked){
      checkedValue = checkedRadio[i].value;
   }
}
Dale
Thank u Dale, but I don't want to know the value of the checked radio. I already know it and also make my php to know it. but how to send my radio name to the php page using myAjaxPostrequest.send(what should i put here??);?
What ajax engine are you using to post? You will need to know what the syntax is inorder to assign values to parameters. Normally it is something like myAjaxPostrequest.send(param1: 'radioName', param2: 'othervalue'); for the param syntax.
Dale
+1  A: 

By following way you can pass parameter to ajax call:

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);
Pranay Rana