动态加载信息到Twitter Bootstrap模式

时间:2012-06-12 19:41:36

标签: php javascript jquery mysql sql

问题:

我想通过使用jQuery将链接属性中的值传递给PHP / SQL查询。

HTML code:

<a data-toggle="modal" href="#myModal" id="1"><i class="pull-right icon-eye-open"></i>HTML</a>

PHP代码:

<div id="myModal" class="modal hide fade">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Title</h3>
    </div>
    <div class="modal-body">            
        <?php
            $query = "SELECT * FROM table WHERE id = ID FROM JQUERY HERE";
            $result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
        ?>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
    </div>
</div>

情境:

当用户单击具有data-toggle =“modal”的link-element时,jQuery应该获取id-attribute的值(在本例中为1)并将其发送到SQL查询,以便SQL查询看起来像:

$query = "SELECT * FROM table WHERE id = 1";

jQuery代码:

$("a[data-toggle=modal]").click(function(){
    var essay_id = $(this).attr('id');
    //Find $essay set it to essay_id in PHP
    //Alternatively create a $_SESSION['EID'] here
});

问题:

如何使用jQuery在PHP中设置变量($ essay)?或者如何通过jQuery在PHP中创建会话变量?

3 个答案:

答案 0 :(得分:21)

这是解决方案,

<a href="#" id="1" class="push">click</a> 

在你的模态体上使用div,就像这样

    <div class="modal-body">  

             <div class="something" style="display:none;">
                    // here you can show your output dynamically 
             </div>
    </div>

现在将数据放入.something并使用ajax调用。请检查http://api.jquery.com/jQuery.ajax/以了解有关jquery ajax的更多信息。

   $(function(){

   $('.push').click(function(){
      var essay_id = $(this).attr('id');

       $.ajax({
          type : 'post',
           url : 'your_url.php', // in here you should put your query 
          data :  'post_id='+ essay_id, // here you pass your id via ajax .
                     // in php you should use $_POST['post_id'] to get this value 
       success : function(r)
           {
              // now you can show output in your modal 
              $('#mymodal').show();  // put your modal id 
             $('.something').show().html(r);
           }
    });


});

   });

答案 1 :(得分:8)

最终解决方案

HTML code:

<div id="myModal" class="modal hide fade">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Title</h3>
    </div>
    <div class="modal-body">            
        <div id="modalContent" style="display:none;">

        </div>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
    </div>
</div>  

jQuery代码:

$("a[data-toggle=modal]").click(function() 
{   
    var essay_id = $(this).attr('id');

    $.ajax({
        cache: false,
        type: 'POST',
        url: 'backend.php',
        data: 'EID='+essay_id,
        success: function(data) 
        {
            $('#myModal').show();
            $('#modalContent').show().html(data);
        }
    });
});

答案 2 :(得分:1)

  1. 使用jQuery获取ID
  2. 通过AJAX将其传递给某个脚本
  3. 取回您的结果,并随心所欲地与您合作
  4. http://api.jquery.com/jQuery.ajax/