这个 ajax 函数获取一个文本框值并将其发送到"response.php"
:(这是ajax.js
的主函数)
function ajaxFunction() {
var getdate = new Date();
if(xmlhttp) {
var txtname = document.getElementById("txtname");
xmlhttp.open("POST","response.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + txtname.value);
}
}
我正在尝试向我的表单添加一个单选按钮集,所以我将其更改为:
function ajaxFunction() {
var getdate = new Date();
if(xmlhttp) {
var txtname = document.getElementById("txtname");
var radio = document.getElementById("radio2"); //ADDED
xmlhttp.open("POST","response.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + txtname.value);
xmlhttp.send("radio=" + radio.value); //ADDED
}
}
作为回应.php
:<?php
if (isset($_POST['txtname'])){
$radio = $_POST['radio'];
//process...
}
?>
输入文本仍然有效,但单选按钮不起作用。
将 post 参数绑定到一个中并发布
var parameters = 'radio='+radio.value+'&txname='+txtname.value;
xmlhttp.send(parameters);
您的变量名称似乎不是"radio2"而是"radio"。
<?php
if (isset($_POST['txtname'])){
// $radio = $_POST['radio2']; // <-- here
$radio = $_POST['radio']; // fixed
//process...
}
?>
因为您以"无线电"的名称发布数据。
xmlhttp.send("radio=" + radio.value); //ADDED
-----在下面添加了我的答案
----请尝试...
xmlhttp.send("txtname=" + txtname.value + "&radio="+ radio.value);
或
xmlhttp.send("txtname=" + txtname.value + "&radio2="+ radio.value);
因为你不能使用 xmlhttp.send() 两次。
我希望这对你有所帮助。