本质上,我有一系列按钮用于过滤数据表。
我的前两个按钮可以正常工作:
<button onclick="filter('open');" class="open">Open</button>
<button onclick="filter('closed');"class="closed">Closed</button>
该函数如下所示:
function filter(st, location) {
if(st == "") {
document.getElementById("exams").innerHTML = "";
return;
} else {
if(window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("exams").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "ajax.php?st="+st+"&location="+location, true);
}}
在 ajax.php 文件中,我检查 st 参数以设置我在查询中使用的变量:
if (isset($_GET["st"])) {
$status = $_GET["st"];
} else {
$status = null;
}
$query = "SELECT examID, `status`, site, first_name, last_name, exam_name, institution FROM exams WHERE status = '{$status}'";
现在我想根据另一个按钮单击向其传递另一个参数以进一步过滤数据。我不确定要为参数添加什么才能使其正常工作。
<button onclick="filter();" class="new-york">New York</button>
最佳答案
我想如果我要做这样的事情,我可能会考虑使用data
属性。通过这种方式,您可以传递多个属性以及操作值。那里有一些可扩展性:
<button class="mybutton" data-instructions='{"action":"open","param1":"something","param2":"something else"}'>Open</button>
在ajax上,将数据对象作为$_POST
(或$_GET
)发送,然后您可以相对轻松地在PHP端获取它。如果您这样做,您只需执行一个在 mybutton
类上触发的操作。您可能可以在其中进行一些自动化编程。
另一种方法可能是使用 innerHTML
从单击的元素内部获取内容,然后正常将其传递给 php。
旁注,如您所知,您的 sql 是开放注入(inject)的。
关于javascript - PHP/Ajax - 多个 GET 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382196/