【问题标题】:Passing modal id from a query so it can be used in another query从查询中传递模态 ID,以便可以在另一个查询中使用
【发布时间】:2015-03-09 11:06:15
【问题描述】:

我想知道如何在模态框上传递选定的 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'] 来自我的第一个查询。

我希望我可以使用 idNum($row['marriage_id']) 中的值,以便在第二个查询中使用它。

我想在这个查询中使用它。

SELECT * FROM marriage WHERE marriage_id = idNum

结果将是从第一个查询中选择的id,它将是我在第二个查询中的 where 子句的参数。

【问题讨论】:

  • 这应该很容易。我很难在这里看到大局。当有人单击链接时,您可以使用 Ajax 将 ID 号发布到服务器。如果您使用mysqliPDO,我无法弥补。
  • 总体情况是,从查询表中,表中选定行的结果将以模态显示。 @Mouser

标签: javascript php mysql sql bootstrap-modal


【解决方案1】:

此示例使用异步数据加载。用户单击按钮并向服务器发送请求,当服务器响应时,程序的下一位将被执行。在您的情况下,打开一个包含婚姻数据的模式。这个系统的优点是用户可以在请求被解决的同时继续使用你的网页。

使用 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>

使用以下函数将脚本块添加到您的页面。

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 文件中,您执行查询并将 echo 响应作为 xmlJSON

//open the modal dialog or update it.: 在这一行下方,您可以检索数据。此行位于检查数据是否已加载的事件处理程序中。由于服务器在这个 if 子句中以200 响应,我们可以假设有响应。如果是 JSON,请使用 JSON.parse(this.responseText)。如果 xml 使用this.responseXML

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多