我有这个表格:
<form onsubmit="serverconnect('Div1', 'query.php'); return
false;"
Minimum Discount: <br />
<input type="radio" value="20%" name="discount" /> 20% <br />
<input type="radio" value="15%" name="discount" /> 15% <br />
<input type="radio" value="10%" name="discount" /> 10% <br />
<input type="radio" value="5%" name="discount" /> 5% <br />
<input type="submit" value="Refine Search" />
</form>
提交时,表单调用以下 ajax 脚本
<script language="javascript" type="text/javascript">
var xmlhttp = false;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function serverconnect(divID, datalocation)
{
var1=$clickvalue;
if(xmlhttp)
{
var obj = document.getElementById(divID);
xmlhttp.open("GET", datalocation);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 &&
xmlhttp.status == 200)
{
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
}
</script>
我需要将表单数据传递到我的 query.php 页面(这就是 ajax 的输出)来修改其输出,而不使用 jquery 库。
那么有没有办法将表单数据通过ajax函数传递到函数调用的页面呢?
最佳答案
是的,您可以使用ajax
传递表单数据。当您使用 GET
方法时,您只需将数据嵌入到页面中,如下所示
<input type="radio" value="20%" name="discount" id="discount" /> 20% <br />
var obj = document.getElementById(divID);
var disc = document.getElementById("discount");
xmlhttp.open("GET", datalocation + "?disc="+disc);
并在query.php中使用$_GET['disc']
来获取get
数据。
关于php - 如何在 ajax 脚本中将变量从表单传递到 php 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684104/