Php 将文本绑定到modalbox中的特定id
我有一个while循环,它从mysql数据库中回送3个随机行。每行包含一个id和一个描述。在我的前端,每一行都显示有id和一个非常简短的描述文本。每行都有一个按钮,用于调用引导模式框,您可以在其中读取更长的描述。这就是它的样子: 我的while循环输出随机id和描述,当你点击readmore时弹出的窗口工作正常。但是在modalbox中,我需要绑定属于id fx id的描述:319需要在modalbox中回显testte 我试着按照这个小标题下的解释来解决问题: ,我走得相当远 如何在my file.php中生成一个select语句来输出描述,该描述属于我单击的单个按钮的id 我在这里发布的代码相当长。我试图减少一些,但我认为一切都是相关的。如果没有,请给我一个提示 选择_shuffle.php 下面的文件我不知道如何将我选择的id绑定到描述 file.php 现在,当您使用jQuery选择器时,如 $'.read button'.单击函数{console.log$this.attr'id';}Php 将文本绑定到modalbox中的特定id,php,twitter-bootstrap,bootstrap-modal,Php,Twitter Bootstrap,Bootstrap Modal,我有一个while循环,它从mysql数据库中回送3个随机行。每行包含一个id和一个描述。在我的前端,每一行都显示有id和一个非常简短的描述文本。每行都有一个按钮,用于调用引导模式框,您可以在其中读取更长的描述。这就是它的样子: 我的while循环输出随机id和描述,当你点击readmore时弹出的窗口工作正常。但是在modalbox中,我需要绑定属于id fx id的描述:319需要在modalbox中回显testte 我试着按照这个小标题下的解释来解决问题: ,我走得相当远 如何在my fi
将输出类似于按钮319的内容。您也可以在按钮中设置其他数据属性,如data id=319,并使用jQuery attr获取该数据。关键是,您需要知道单击了哪个按钮并传递id。请注意,您的问题不清楚。你想要实现什么?谢谢你的评论。我编辑了我的问题。所以基本上我的问题是:我如何在我的file.php中生成一个select语句来输出描述,它属于我单击的单个按钮的id?所以你想要一个数组,其中key作为id,description在值中?你应该解释你想做什么。点击按钮,说明会显示在模式框中?有两种方法。您可以从html中已有的描述中获得它,也可以通过传递id对描述进行ajax调用。因此,问题变成了如何在单击按钮时传递id。您可以通过为每个按钮分配id来实现这一点。是的,当按钮id为319时,这是正确的,我需要得到同一行中的描述。所以每个id都有不同的描述。jQuery选择器应该在哪里?在选择_shuffle.php?php文件呢?jQuery在前端工作。因此,您可以将它与页面上的其他Java脚本一起使用。
<!-- Everything is working in this file -->
<?php
$sql = "SELECT id, description FROM stores ORDER BY RAND ( ) LIMIT 3";
$res = $mysqli->query($sql);
//print($res);
if ($res->num_rows > 0) {
// output data of each row
while($row = $res->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>" .
"Description: " . $row["description"]. "<br><br>".
'<span data-placement="top" data-toggle="tooltip" title="Show"><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#editBox" data-id="<?php echo $obj->id;?>"><span class="glyphicon glyphicon-pencil"></span></a></span>';
}
} else {
echo "0 results";
}
?>
<div class="modal fade" id="editBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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"><center>Heading</center></h4>
</div>
<div class="modal-body">
<div class="form-data">
//Here Will show the Data
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default">Select</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
$( document ).ready(function() {
$('#myModal').on('show.bs.modal', function (e) { //Modal Event
var id = $(e.relatedTarget).data('id'); //Fetch id from modal trigger button
$.ajax({
type : 'post',
url : 'file.php', //Here you will fetch records
data : 'post_id='+ id, //Pass $id
success : function(data){
$('.form-data').html(data);//Show fetched data from database
}
});
});
});
</script>
<?php
include 'dbconnection.php';
if($_POST['id']) {
$id = $_POST['id'];
$sql = "SELECT id, description FROM stores";
echo $sql;
else {
echo "0 results";
}
?>
<button class='read-button' type='button' id="button-<?php echo $row['id'];?>">Read more</button>