将多个变量传递给bootstrap模式

时间:2013-07-11 05:06:51

标签: php ajax twitter-bootstrap modal-dialog

我可以将多个变量传递给bootstrap模式吗?

这里是我的代码:

p>Link 1</p>
<a data-toggle="modal" id="1" class="push" href="#popresult">test</a>
<p>&nbsp;</p>
<p>Link 2</p>
<a data-toggle="modal" id="2" class="push" href="#popresult">test</a>

点击链接会弹出一个bootstrap模态

<div class="modal hide" id="popresult">
  <div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body" style="display:none;">
    <p>some content</p>

  </div>
  <div class="modal-footer"> <a href="#" class="btn" data-dismiss="modal">Close</a> <a href="#" class="btn btn-primary" >View full result</a> </div>
</div>

这里的ajax代码:

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

       $.ajax({
          type : 'post',
           url : 'yourScript.php', // in here you should put your query 
          data :  'post_id='+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 
             $('.modal-body').show().html(r);
           }
      });
   });
});

ajax代码会将变量发布到另一个php文件yourScript.php

<?php
 session_start();
     $_SESSION['myId'] = $_POST['post_id'];
     echo $_SESSION[myId];
?>

如何将多个变量传递给yourScript.php?

2 个答案:

答案 0 :(得分:2)

您可以传递多个这样的变量

var param1 = 2;
var param2 = 5;

var data = 'param1='+param1+'&param2='+param2;

$.ajax({
         type: "POST",
         url: "yourScript.php",
         data: data,
         success: function(html){

         }
       });

SEE THIS EXAMPLE如果您想从<a>代码中获取更多数据,而不是仅获取id

答案 1 :(得分:0)

发布多个参数的最简单方法是使用对象文字在ajax调用中设置数据元素

data: {
        'post_id':id,
        'myParam':"myvalue",
        'anotherOne':"again"
}

然后你可以在php

中以$ _POST值的形式访问它们
$post_id = $_POST["post_id"];
$myParam = $_POST["myParam"];
$anotherOne = $_POST["anotherOne"];