通过Ajax引导模式中的PHP变量
我有一个php页面,其中列出了数据库中的一组id。然后,我在每个id旁边都有一个按钮,它将在页面中打开一个Twitter引导模式窗口,我需要将php变量传递给该模型窗口,以便在其中执行mysql查询 我的问题是模型窗口只是显示我设置的文本框,它应该显示唯一的id。但是,它显示的文本框中只有一个“null” 我的代码在下面 用于触发模式窗口的链接。注意:我确认下面的链接确实显示了php变量的值通过Ajax引导模式中的PHP变量,php,ajax,Php,Ajax,我有一个php页面,其中列出了数据库中的一组id。然后,我在每个id旁边都有一个按钮,它将在页面中打开一个Twitter引导模式窗口,我需要将php变量传递给该模型窗口,以便在其中执行mysql查询 我的问题是模型窗口只是显示我设置的文本框,它应该显示唯一的id。但是,它显示的文本框中只有一个“null” 我的代码在下面 用于触发模式窗口的链接。注意:我确认下面的链接确实显示了php变量的值 <a href='#switch_modal' class='btn btn-default bt
<a href='#switch_modal' class='btn btn-default btn-small' data-toggle='modal' data-id='$scommentid'>REPLY</a>
将php变量放入模式窗口的Ajax代码
<!-- get the unique comment id to use in the modal window -->
<script>
$(document).ready(function(){
$('#switch_modal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'fetch_comment_id.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data)
{
$('.fetched-data').html(data);//Show fetched data from database
}
});
});
});
</script>
$(文档).ready(函数(){
$('#switch_modal').on('show.bs.modal',function(e){
var rowid=$(e.relatedTarget).data('id');
$.ajax({
键入:“post”,
url:‘fetch_comment_id.php’,//您将在这里获取记录
数据:“rowid=”+rowid,//传递$id
成功:功能(数据)
{
$('.fetched data').html(data);//显示从数据库中提取的数据
}
});
});
});
这是我的模态窗口代码
<div class="modal fade" id="switch_modal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<div class="fetched-data"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
&时代;
编辑数据
接近
下面是我的fetch_comment_id.php文件
<?php
$id = $_POST['rowid']; //I know I need to clean this variable
echo "# <input type='text' name='acommentid' value='$id'>";
?>
试试这个:
将锚定标记中指定的数据id属性替换为以下内容:
data-id='<?php echo $scommentid; ?>'
数据id=''
更改
var rowid = $(e.relatedTarget).data('id');
到
您的代码应该如下所示:
$('#switch_modal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).attr('data-id');
$.ajax({
type : 'post',
url : 'fetch_comment_id.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data) {
$('.fetched-data').html(data);//Show fetched data from database
}
});
});
您可以在下面看到此工作的演示。请注意,我将rowid
传递给html()
而不是data
,以表明它正确传递了值
我们需要更清晰的信息。好吧,把我想制作的页面想象成一个电子邮件收件箱。在收件箱类型的php/mysql页面上,我有一个从数据库中提取的消息名称和消息id的列表。这一切都很好。现在,我在每一行上都有一个按钮,它将打开一个Twitter引导模式窗口,我打算在其中显示用户单击的消息。因此,我需要一种方法,当您单击我在上面的消息中的链接时,使用ajax将我的a href链接中的数据id值获取到我的模式窗口中。我仍在寻找解决此问题的建议解决方案。任何人:(你能使用JSFIDLE或jsbin创建一个最小的、完整的、可验证的示例吗?我认为这将有助于每个人理解这个问题。听起来你可以通过模拟PHP正在创建的HTML来创建这个没有PHP的示例。错误必须在其他地方。我刚刚重建了这个设置,一切都很好。你应该尝试一下调试代码。例如,在var rowid=$(e.relatedTarget).data('id');
doalert(rowid);
之后,查看是否从javascript代码中获取rowid。或者在PHP代码中更改$id=$\u POST['rowid'];
到$id=$\u请求['rowid']
。然后在浏览器中打开它,在url中传递变量:fetch_comment_id.php?rowid=1234
,然后查看php脚本返回的内容。感谢Shivani Patel的建议,但是data id=''这一行被包装在一个标记中。因此,当我查看页面源代码时,data id=确实包含正确的值。我的问题是fetch\c在我看来,omment_id.php文件似乎没有获取数据id的值,因此我不确定是我的javascript/ajax错误还是fetch_comment_id.php文件错误
$('#switch_modal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).attr('data-id');
$.ajax({
type : 'post',
url : 'fetch_comment_id.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data) {
$('.fetched-data').html(data);//Show fetched data from database
}
});
});