将PHP变量检索到Bootstrap Modal

时间:2015-10-09 09:04:01

标签: javascript php html5 bootstrap-modal bootstrapping

好的,所以我正在开发一个类项目,我正在构建一个具有基本功能的网站。我是php和javascript的新手。

所以这就是问题所在。我在phpMyAdmin中创建了一个名为'itemdb'的数据库。我可以通过html + php添加项目并删除它们。现在我想要做的是编辑数据库中的数据,这是我无法找到它的修复程序。

这就是我的想法。 1.点击编辑按钮。 2.打开Bootstrap Modal。 3.显示项目信息。 4.点击保存更改。数据库已更新。

我的问题是如果我将按钮/输入类型设置为“提交”,并使用表单标记,模式将崩溃。 但是,下面的当前代码没有表单标签,按钮设置为“按钮”类型,当我单击按钮时,即使单击另一个按钮(相同数据),它也只显示一个项目的信息。

这是我的代码。

出于测试目的,我创建了一个在第一行显示“hello”的表和第二行中的一个按钮,当我单击该按钮时,它将在模态中显示信息。

 <div class = "row col-xs-10 col-sm-10 col-md-10 col-lg-10 ">
<?php
include 'connectDB.php';
$query = "SELECT * FROM `itemdb`;";
$result = mysqli_query($dbconnect, $query);
if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_assoc($result))
    {?>
    <table>
        <tr>
            <td>Hellow</td>
            <td>
                <?php
                echo "
                <button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#exampleModal\" 
                data-whatever=\"editDB[" .  $row['itemID'] . "]\" onclick=\"window.Socation.href = 'adminItems3.php?value=" .  $row['itemID'] . "'\";>Edit</button>
                ";?>
                <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                            </div>
                            <div class="modal-body">
                                <?php 
                                if(!empty($row['itemID'])){
                                $ID = $row['itemID'];
                                echo $ID;

                                $query1 = "SELECT * FROM `trademe`.`itemdb` WHERE `itemdb`.`itemID` = $ID;";
                                $result1 = mysqli_query($dbconnect, $query1);
                                if(mysqli_num_rows($result1) > 0){
                                $row1 = mysqli_fetch_assoc($result1);

                                echo "
                                <table class=\"table-striped\">
                                <tr>
                                <td class = \"imgBoxCol\">
                                <img class=\"imgBox img-rounded imgBox\"src=\"" . $row1['img_path'] . "\"></img>
                                </td>
                                <td>
                                <p >Ad Title: <input name=\"editadTitle\" value=\"" . $row1['adTitle'] . "\"/></p> 
                                <p>Item Name:<input name=\"edititemName\" value=\" " . $row1['itemName'] . "\"/> </p>
                                <p>Condition:<textarea name=\"editcond\" type=\"text\" >" . $row1['cond'] . "</textarea></p>
                                <p>Description:<textarea name=\"editdescription\"  type=\"text\" > " . $row1['description'] . " </textarea></p>
                                <p>Method:<input name=\"editmethod\" value=\"" . $row1['method'] . " \"/></p>
                                <p>Category:<input name=\"editcategory\" value=\"" . $row1['category'] . " \"/></p>
                                </td>
                                </tr>

                                </table>
                                ";
                                }}?>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Send message</button>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <?php//opening php// ending html code.  
    }
}
?>
</div>

脚本

$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var Eid = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + Eid)
   modal.find('.modal-body input').val(Eid)
  // document.location="adminItems3.php?idSelected=" + Eid
})

我一直试图解决这个问题。如果你们有更好的想法,至少以某种方式传递项目ID,那么我的模态将能够检索它,那将是非常好的!

有没有办法传递变量,以便我可以通过我的模态检索它而不会崩溃?

[点击时第3个按钮的图像] [2]

显示第一个按钮的信息 再次感谢 !!

enter image description here

2 个答案:

答案 0 :(得分:1)

我已经给出了打开引导模式对话框的最简单方法。我会帮你的。按照这个冷静地工作。保持耐心。按照原样遵循代码。它会对你有所帮助。

$target=$_REQUEST['target'];
switch(target)
{
case 'book': //your delete query for tbl_book
break;
case 'emp': //your delete query for tbl_emp
break;
}

页脚中,放置此代码

<div class = "row col-xs-10 col-sm-10 col-md-10 col-lg-10 ">
    <?php
    include 'connectDB.php';
    $query = "SELECT * FROM `itemdb`;";
    $result = mysqli_query($dbconnect, $query);
    if(mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_assoc($result))
        {?>
        <table>
            <tr>
                <td>Hellow</td>
                <td>
                    <a class="ItemID" href="#exampleModal" data-toggle="modal" data-whatever="<?echo $row['itemID'];?>">
                        <input type='button' class='btn btn-primary' value="Edit">
                    </a>
                </td>
            </tr>
        </table>
        <?php//opening php// ending html code.  
        }
    }
    ?>
</div>

脚本

<div id="exampleModal" class="modal fade" aria-hidden="true" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>

<强> NewPage.php
([注意:请记住,此页面名称也用于脚本标记。因此,如果您更改此页面名称。您必须更改我给出的脚本标记中的相同页面名称])

<script>
    $('.ItemID').click(function(){
        var ItemID=$(this).attr('data-whatever');
        $.ajax({url:"NewPage.php?ItemID="+ItemID,cache:false,success:function(result){
            $(".modal-content").html(result);
        }});
    });
</script>

答案 1 :(得分:0)

所有模态都具有相同的ID“exampleModal”,因此只有在触发模态时才显示第一个模式。

尝试将ID设为

$myModalID = "exemplaModal_".$row['ID'];

中的

data-target=\"#<?php echo $myModalID?>\"  //I forget to let the hastag here, my bad

AND

class="modal fade" id="<?php echo $myModalID?>".

PS:你有错误

onclick=\"window.Socation.href"

它不是社交而是位置

编辑:

<div class = "row col-xs-10 col-sm-10 col-md-10 col-lg-10 ">
<?php
include 'connectDB.php';
$query = "SELECT * FROM `itemdb`;";
$result = mysqli_query($dbconnect, $query);

if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
// HERE you declare your modal ID
$myModalID = "exemplaModal_".$row['ID'];
?>
<table>
    <tr>
        <td>Hellow</td>
        <td>
            <?php
            ///!\ HERE : Dont forget to let the hashtag in data-taget : data-target="# <---
            echo "
            <button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#<?php echo $myModalID?>\" 
            data-whatever=\"editDB[" .  $row['itemID'] . "]\" onclick=\"window.Socation.href = 'adminItems3.php?value=" .  $row['itemID'] . "'\";>Edit</button>
            ";
            ?>

            <!-- And Here juste add the normal echo $myModalID -->
            <div class="modal fade" id="<?php echo $myModalID?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                        </div>
                        <div class="modal-body">
                            <?php 
                            if(!empty($row['itemID'])){
                            $ID = $row['itemID'];
                            echo $ID;

                            $query1 = "SELECT * FROM `trademe`.`itemdb` WHERE `itemdb`.`itemID` = $ID;";
                            $result1 = mysqli_query($dbconnect, $query1);
                            if(mysqli_num_rows($result1) > 0){
                            $row1 = mysqli_fetch_assoc($result1);

                            echo "
                            <table class=\"table-striped\">
                            <tr>
                            <td class = \"imgBoxCol\">
                            <img class=\"imgBox img-rounded imgBox\"src=\"" . $row1['img_path'] . "\"></img>
                            </td>
                            <td>
                            <p >Ad Title: <input name=\"editadTitle\" value=\"" . $row1['adTitle'] . "\"/></p> 
                            <p>Item Name:<input name=\"edititemName\" value=\" " . $row1['itemName'] . "\"/> </p>
                            <p>Condition:<textarea name=\"editcond\" type=\"text\" >" . $row1['cond'] . "</textarea></p>
                            <p>Description:<textarea name=\"editdescription\"  type=\"text\" > " . $row1['description'] . " </textarea></p>
                            <p>Method:<input name=\"editmethod\" value=\"" . $row1['method'] . " \"/></p>
                            <p>Category:<input name=\"editcategory\" value=\"" . $row1['category'] . " \"/></p>
                            </td>
                            </tr>

                            </table>
                            ";
                            }}?>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Send message</button>
                        </div>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>
<?php//opening php// ending html code.  
}
}