【发布时间】:2014-08-05 16:49:24
【问题描述】:
我使用 jQuery 通过 span 标签将变量传递给 Bootstraps 模态,但在模态中运行 PHP 时出现问题。这些变量都正常通过,但是当我尝试运行 mySQL 语句时,我存储在变量中的 span 标签显示为空,即使它回显很好。我已经给出了下面的代码示例:
HTML:
<a role='button' data-topicid='$number' data-toggle='modal' data-target='#modaluserinfo' class='modalsend'>
JS:
<script>
$(document).ready(function () {
$('.modalsend').click(function () {
$('span.user-topicid').text($(this).data('topicid'));
});
});
</script>
模态:
<div class="modal fade" id="modaluserinfo" tabindex="-1" role="dialog" aria-labelledby="modaluserinfo" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Details...</h4>
</div>
<div class="modal-body" style="text-align:center; margin-top:10px;">
<?php
$numberid = "<span class='user-topicid'></span>";
echo $numberid;// THE VALUE ECHOS FINE IN THE MODAL, BUT FAILS TO LOAD IN THE MYSQL QUERY BELOW
$sqlcomment = mysql_query("SELECT * FROM comments WHERE comments.topic_id='$numberid' AND comments.emotions_id='1'");
$commentnumber = mysql_num_rows($sqlcomment);
echo $commentnumber;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
我的猜测是 PHP 语句在页面加载时运行,而不是在打开模式并将变量发送到 span 标签时运行。不过不太清楚如何解决这个问题。
【问题讨论】:
-
PHP 只在服务器上运行。因此,如果您想在页面加载后执行 PHP 脚本,并且不重新加载页面,请使用 XHR 调用服务器上的 PHP 脚本。然后 PHP 脚本可以通过 XHR 对象将信息发送回浏览器。
-
我不太确定 XHR 是什么,我希望有另一种解决方案,因为变量 $numberid 在模态中回显很好,但在 MySQL 语句 $sqlcomment 中没有加载
标签: javascript php jquery html twitter-bootstrap