【问题标题】:Retrieving PHP variables to Bootstrap Modal将 PHP 变量检索到 Bootstrap 模式
【发布时间】:2015-10-09 09:04:01
【问题描述】:

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

所以这就是问题所在。我在 phpMyAdmin 中创建了一个名为“itemdb”的数据库。我可以通过 html+php 向其中添加项目并删除它们。现在我想做的是编辑数据库中的数据,这就是我找不到修复它的地方。

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

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

这是我的代码。

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

 <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,以便我的模态能够检索它,那就太好了!

有什么方法可以传递变量,这样我就可以通过我的模态检索它而不会崩溃?

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

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

enter image description here

【问题讨论】:

    标签: javascript php html bootstrap-modal bootstrapping


    【解决方案1】:

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

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

    在脚本中

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

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

    <?
    include 'connectDB.php';
    ?>
    <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($_GET['ItemID']))
        {
            $ID = $_GET['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>
    

    【讨论】:

    • 非常感谢您的回复先生,我会努力工作的,我回到家。一定会回复你的! :D
    • 是的,当然@Chocobie95。但是,冷静地工作。好的。否则,任何遗漏的东西......你会被这些代码搞砸的。所以,一步一步的工作。祝一切顺利。希望,它会有所帮助。谢谢
    • 嘿先生,我已经尝试使用您的代码。当我现在单击按钮时,只弹出模态的黑色褪色背景,而不是模态体。所以它没有显示任何内容,而且我看不到任何变量通过 url 传递给变量。有什么我应该改变的吗?
    • 昨天,我回答了他的一个问题。它在那里工作得很好。检查一次。 stackoverflow.com/questions/33043115/…
    • 是的。你的模态正在工作。它会来。检查控制台,会出现什么错误告诉我@Chocobie95
    【解决方案2】:

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

    尽量让ID像

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

    在两者中

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

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

    【讨论】:

    • 您好,谢谢您的回复。我有几个问题。 $myModalID = "exemplaModal_".$row['ID']; 我需要在我的 php 中添加它吗?如果是,我已经尝试了您给出的步骤,但它仍然无法正常工作。现在它甚至没有显示模态。有没有办法让我通过 get 方法传递变量并在我的 php 中检索它?顺便说一句,onclick 函数用于测试其他一些东西。
    • 我已经用代码进行了编辑。重要的部分是当你在data-target 中声明模态 ID 时不要忘记 hastag。我的坏。
    • 我已经按照您的方式完成了,但仍然没有显示。我相信这一定是由于javascript?我应该如何将它显示到 javascript 中?这就是我显示它的方式...&lt;script&gt; var con="#"; var aidei="&lt;?php echo $myModalID; ?&gt;"; var exid= con.concat(aidei); $(exid).on('show.bs.modal', function (event) {
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2021-01-13
    • 2019-12-31
    • 2019-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多