简体   繁体   English

使用AJAX发布表单

[英]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 此代码用于使用ajax发布表单

and my php page needs the name of the radiobutton clicked 我的php页面需要点击radiobutton的名称

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. 我要插入的是位于radiobutton名称的brakects之间的数字。

I could do it when I post form without AJAX but now I can't send the name of the radiobutton 当我在没有AJAX的情况下发布表单但是现在我无法发送radiobutton的名称时,我可以这样做

any clue about what I can send as parameter for function myAjaxPostrequest.send(parameters); 任何关于我可以作为函数myAjaxPostrequest.send(parameters);参数发送的线索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: 通过以下方式,您可以将参数传递给ajax调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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