我想知道如何在模式上传递选定的 ID。
下面我有位于表格内的代码:
<a type='button' id='seemore' data-toggle='modal' idNum='".$row['marriage_id']."'
data-target='#see_more' class='glyphicon glyphicon-eye-open'></a>
$row['marriage_id']
来 self 的第一个查询。
我希望我可以使用 idNum($row['marriage_id'])
中的值,这样我就可以在第二个查询中使用它。
我想在此查询中使用它。
SELECT * FROM marriage WHERE marriage_id = idNum
结果将是从第一个查询中选择的 id
,它是第二个查询中我的 where 子句的参数。
最佳答案
此示例使用异步数据加载。用户单击按钮并向服务器发送请求,当服务器响应时执行程序的下一部分。在您的情况下,打开一个包含婚姻数据的模式。该系统的优点是用户可以在解决请求的同时继续使用您的网页。
使用 onclick 事件更新 HTML 中的链接。
<a type='button' id='seemore' data-toggle='modal' idNum='".$row['marriage_id']."'
data-target='#see_more' onclick='sendRequest(this.idNum)'
class='glyphicon glyphicon-eye-open'> </a>
使用以下函数将脚本 block 添加到您的页面。
JavaScript:
function sendAjax(id, e) {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200){
//request is complete and loaded
//open the modal dialog or update it.
}
else if(xmlhttp.status == 400) {
alert('There was an error 400')
}
else {
alert('something else other than 200 was returned')
}
}
}
xmlhttp.open("POST", "file.php", true); //true means asynchronously.
xmlhttp.send("idnum="+id);
}
id
作为 post var 发送到服务器。将 file.php
替换为您自己的 php 文件。在该 php 文件中,您执行查询并以 xml 或 JSON 形式echo
响应。
//打开模态对话框或更新它。
:在此行下方,您可以检索数据。此行位于检查数据是否已加载的事件处理程序中。由于服务器在此 if 子句中以 200
响应,我们可以假设有响应。如果是 JSON 使用 JSON.parse(this.responseText)
。如果 xml 使用 this.responseXML
。
关于javascript - 从查询中传递模态 ID,以便它可以在另一个查询中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27889170/