繁体   English   中英

通过PHP将MySQL数据传递给模态形式

[英]Passing MySQL data to Modal Form via PHP

我正在尝试使用模式作为用户在MySQL数据库中编辑数据的简单方法。 看来只有在SELECT语句中检索的第一行可用于模式,而我不明白为什么。 换句话说,想象一个有几列的表,第一列是客户的名字。 我的目标是能够单击名称,显示带有表单的模式,然后传递该表单的结果以使用PHP更新数据库。 但是,无论我单击哪个名称,都只会传递与第一行结果有关的值。

表格“项目”包括表格中所有行的信息。

$personID是通过GET传递的,是选择特定员工的客户的方式。

模态div包含在PHP while子句中。

模态中包含的表单将信息传递给基本的PHP更新脚本。

非常感谢,迈克

PHP选择语句:

    <?php
        $query = "SELECT * FROM item WHERE personID = $personID";
        $result = mysql_query($query);
        while($info=mysql_fetch_array($result)){
        $itemID = $info['itemID'];
    ?>  

链接(许多链接之一;我将本节缩写为):

    <div class='row-fluid'>
        <div class='span3'>
            <a href="#customerModal" data-toggle="modal"><?php echo($info['itemCustomer']); ?></a>
        </div>
    </div>  

莫代尔:

<div id="customerModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel"><?php echo($info['itemID']); ?></h3>
</div>
<div class="modal-body">
    <form action='pipelineEdit.php' method='post'>
        <input placeholder='Customer Name' style='width:50%' type='text' name='editValue' id='editValue'>
        <input type='hidden' value='itemCustomer' name='editField' id='editField'>
        <input type='hidden' value='<?php echo($info['itemID']); ?>' name='itemID' id='itemID'>
        <input type='hidden' value='<?php echo($personID); ?>' name='personID' id='personID'>
</div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            <button class="btn btn-primary">Save changes</button>
          </div>
          </form>
        </div>

PHP的结束while配备模式后:

    <?php } ?>

您正在打印具有相同ID = customerModal的多个div

ID必须是唯一的,因此链接只会打开循环中打印的第一个模态。

更改:

<div id="customerModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

至:

<div id="customerModal<?php echo $info['itemID']; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

和:

<a href="#customerModal" data-toggle="modal">

至:

<a href="#customerModal<?php echo $info['itemID']; ?>" data-toggle="modal">

运用

while($info=mysql_fetch_array($result)){
    $itemID = $info['itemID'];

表示您继续分配$ itemId。 最后,$ itemId仅保存最后分配的值。

您的模态由ID标识。 在jQuery中,只会引用具有该ID的第一个DOM元素。 要引用正确的模式,可以将数据库ID附加到元素ID上,例如:

<div id="customerModal<?php echo $info['itemID']; ?>"

该链接也需要附加该链接:

<a href="#customerModal<?php echo $info['itemID']; ?>" data-toggle="modal">

有关使用ID选择器的更多信息,请在此处查看jQuery文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM