php - 如何在 ajax 脚本中将变量从表单传递到 php 页面?

标签 php javascript ajax

我有这个表格:

<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/

相关文章:

javascript - Gmail API发送电子邮件错误401 :Invalid Credentials

javascript - 如何在函数中传递不带引号的字符串作为参数?

php - undefined index 传递变量与 javascript 到 php

php - 用 PDO 和准备好的语句替换 mysql_* 函数

php - 带有两个 WHERE 子句的 MySQL SELECT 语句

javascript - 使用嵌套循环构建三 Angular 形

php - 是否有可能获得函数的静态值?

php - 使用 Angular.js 的 HTTP POST

javascript - Ajax 帖子表单 - 如何重新提交?

javascript - 如何使用 AJAX 将八位字节/流类型的 Blob 发送到服务器?