简体   繁体   中英

Posting a form using AJAX

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>

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);

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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM