动态加载信息到Twitter Bootstrap模式 [英] Dynamically load information to Twitter Bootstrap modal
问题描述
问题:
我想通过使用jQuery将链接属性中的值传递给PHP / SQL查询。
I would like to transfer by using jQuery a value in an link-attribute to PHP/SQL query.
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">×</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查询看起来像:
When the user clicks the link-element that has data-toggle="modal" then jQuery should take the value of the id-attribute (which is 1 in this case) and send it to the SQL-query so that the SQL query would look like:
$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中创建会话变量?
How do I use jQuery to set a variable ($essay) in PHP? or how can I create a session variable in PHP through jQuery?
推荐答案
这是解决方案,
<a href="#" id="1" class="push">click</a>
在你的模态体上使用div,就像这样
use a div on your modal body , like this
<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的更多信息。
now put data into the .something
with ajax calling .please check http://api.jquery.com/jQuery.ajax/ to know more about 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);
}
});
});
});
这篇关于动态加载信息到Twitter Bootstrap模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!