简体   繁体   English

在模式引导程序中传递php变量

[英]pass php variable in modal bootstrap

I have this while loop.. 我有这个while循环。

while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview">

<div class="modal fade" id="preview" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                 <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
                   </div>
                 <div class="modal-body">
                      <iframe style="display:block;" frameborder="0"></iframe>
                 </div>
                 <div class="modal-footer">

                         </div>
                </div><!-- /.modal-content -->
             </div><!-- /.modal-dialog -->
          </div><!-- /.modal -->
}

So when i click in the first car of the list my modal popup opens and shows the title normal as it is. 因此,当我单击列表中的第一辆车时,我的模式弹出窗口将打开并按原样显示标题。 When i click on the second car and the modal popup opens it shows me again the title of the first car instead of second car that i clicked on. 当我单击第二辆汽车并打开模态弹出窗口时,它将再次向我显示第一辆汽车的标题,而不是我单击的第二辆汽车的标题。 I tried with jquery to take the title on #tbinfo click with no success: 我尝试使用jquery在#tbinfo click上获得标题,但没有成功:

$('#tbinfo').on('click',function(){
var title = $(this).attr('data-title');
$('.modal-title').html(title);
});

What can i do to fix it? 我该如何解决? any ideas? 有任何想法吗?

In my project I'm using data- attributes. 在我的项目中,我正在使用data-属性。

Example. 例。 Look, how do I pass code to modal. 看,我如何将code传递给模态。

Link: 链接:

<a href="#modal-info" class="text-muted" data-toggle="modal" data-code="{{ code }}">

Modal: 模态:

<div class="modal fade" id="modal-info">
    <dd name="code"></dd>
</div>

Event listener: 事件监听器:

$('#modal-info').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var code   = button.data('code');
    var modal  = $(this);

    modal.find('[name="code"]').text(code);
}

hello you update your code like: 您好,您更新代码如下:

 <?php 
$i=1;
        while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>
        <div class="car-box modalButton tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview_<?php echo $i; ?>">

        <div class="modal fade" id="preview_<?php echo $i; ?>" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">
                      <div class="modal-dialog">
                         <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
                           </div>
                         <div class="modal-body">
                              <iframe style="display:block;" frameborder="0"></iframe>
                         </div>
                         <div class="modal-footer">

                                 </div>
                        </div><!-- /.modal-content -->
                     </div><!-- /.modal-dialog -->
                  </div><!-- /.modal -->
    <?php $i++;    }?>

assign each modal unique id 分配每个模式唯一ID

You just need to increment your html id attributes as its currently always going to reference the first modal created. 您只需要增加html id属性即可,因为它当前总是引用创建的第一个模式。 All id 's must be unique in html. 所有id在html中必须唯一。

I would just add in a simple counter and append it to your id. 我只需要添加一个简单的计数器并将其添加到您的ID中即可。 So create a new variable, in this example $i and assign it the value of 0 . 因此,创建一个新变量,在本例中$i并将其赋值为0 In your loop, increment the value either at the beginning or end of the loop. 在循环中,在循环的开始或结束处增加值。 Then append the value of $i to your modal id attribute and the modalButton 's data-target attribute. 然后将$i的值附加到您的模态id属性和modalButtondata-target属性中。 So preview becomes something like preview-<?=$i;?> in all instances. 因此,在所有情况下, preview变得像preview-<?=$i;?>类的东西。

Here, something like this: 在这里,像这样:

<?php

$i = 0;

while($list = $cars->fetch(PDO::FETCH_ASSOC)) {

    ++$i;
?>

    <div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview-<?=$i;?>">

    <div class="modal fade" id="preview-<?=$i;?>" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">

    ... modal markup ...

Make sure your PHP installation supports the shorthand echo <?=$VAR;?> , if that isn't working then you can just echo the variable out as normal, like you've done else where <?php echo ... ?> . 确保您的PHP安装支持速记回显<?=$VAR;?> ,如果该方法不起作用,则可以像往常一样回显该变量,就像您在<?php echo ... ?>

That will make sure that every time you click the button, it opens a new, unique, modal instead of just the first one. 这样可以确保每次单击按钮时,它都会打开一个新的,唯一的模态,而不是第一个。

Don't use id . 不要使用id Use class name . 使用class name

<?php 
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>

    <div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview"></div>

}?>

Place this code in footer or in same page in down. 将此代码放在页脚或向下的同一页中。

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

      </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

JS JS

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

Create a page name ajax_modalTitle.php (If you are looking to change this page name. Change in <script></script> tag too. Both are related.) 创建一个页面名称ajax_modalTitle.php (如果要更改此页面名称。也请在<script></script>标记中进行更改。两者都相关。)

<?php
$title = $_GET['title'];
?>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel"><?php echo $title;?></h4>
</div>
<div class="modal-body">
    <iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">

</div>

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

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