简体   繁体   English

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

[英]Passing MySQL data to Modal Form via PHP

I am attempting to use modal as a simple means for users to edit data in a MySQL db. 我正在尝试使用模式作为用户在MySQL数据库中编辑数据的简单方法。 It appears that only the first line retrieved in my SELECT statement is available to modal and I do not understand why. 看来只有在SELECT语句中检索的第一行可用于模式,而我不明白为什么。 In other words, imagine a table with several columns, the first being a customer's name. 换句话说,想象一个有几列的表,第一列是客户的名字。 My goal is to be able to click a name, show a modal with a form, and then pass the results of that form to update the db using PHP. 我的目标是能够单击名称,显示带有表单的模式,然后传递该表单的结果以使用PHP更新数据库。 No matter which name I click, though, only values related to the first resulting line are passed. 但是,无论我单击哪个名称,都只会传递与第一行结果有关的值。

Table 'Item' includes information for all of the lines that will be present in the table. 表格“项目”包括表格中所有行的信息。

$personID is passed via GET and is the means of selecting customers of a specific employee. $personID是通过GET传递的,是选择特定员工的客户的方式。

The modal div is included within the PHP while clause. 模态div包含在PHP while子句中。

The form contained in the modal passes the information to a basic PHP update script. 模态中包含的表单将信息传递给基本的PHP更新脚本。

Thanks very much, Mike 非常感谢,迈克

PHP Select Stmt: PHP选择语句:

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

Link (one of many; I've abbreviated this section): 链接(许多链接之一;我将本节缩写为):

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

Modal: 莫代尔:

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

End of PHP while comes after modal: PHP的结束while配备模式后:

    <?php } ?>

You are printing multiple divs with the same ID = customerModal 您正在打印具有相同ID = customerModal的多个div

IDs have to be unique, so the link will only open the first modal printed in the loop. ID必须是唯一的,因此链接只会打开循环中打印的第一个模态。

Change: 更改:

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

to: 至:

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

and: 和:

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

to: 至:

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

Using 运用

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

means you keep assigning to $itemId. 表示您继续分配$ itemId。 In the end $itemId only holds the last assigned value. 最后,$ itemId仅保存最后分配的值。

Your modal is identified with id. 您的模态由ID标识。 In jQuery, only the first DOM element with that id will be referenced. 在jQuery中,只会引用具有该ID的第一个DOM元素。 To reference the correct modal, you can append the database id to the element id such as: 要引用正确的模式,可以将数据库ID附加到元素ID上,例如:

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

The link would need to have that appended as well: 该链接也需要附加该链接:

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

For more information using the ID selector view the jQuery documentation here . 有关使用ID选择器的更多信息,请在此处查看jQuery文档。

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

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