如何从php foreach循环中的div到模式框中获取唯一值

如何从php foreach循环中的div到模式框中获取唯一值,php,Php,我在PHP foreach循环中有一个div元素。在div内部,我有一个打开引导模式框的按钮。模式框在php循环之外。我试图做的是,当用户单击按钮时从div获取ID,然后使用该ID运行查询,以显示模式框上的信息。我不希望模态框循环多次,但无法找到解决问题的方法。因为我一直在模式框中获取最后一个div id 我的html代码: foreach($query as $data){ $id = $data['ID']; $Access = $data['Access']; if(s

我在PHP foreach循环中有一个div元素。在div内部,我有一个打开引导模式框的按钮。模式框在php循环之外。我试图做的是,当用户单击按钮时从div获取ID,然后使用该ID运行查询,以显示模式框上的信息。我不希望模态框循环多次,但无法找到解决问题的方法。因为我一直在模式框中获取最后一个div id

我的html代码:

foreach($query as $data){
   $id = $data['ID'];
   $Access = $data['Access'];
    if(strlen($Access) > 3){
      echo'
           <div class="box" >
           <input type="hidden" value='.$id.' class="ID" >
           <textarea class="Access">'.$Access.'</textarea>
           <i class="fas fa-info-circle"  data-id ='.$id.' data-toggle="modal" data-target="#exampleModal"></i>
 </div>';
foreach($queryas$data){
$id=$data['id'];
$Access=$data['Access'];
如果(strlen($Access)>3){
回声'
“.$Access。”
';
}


情态标题
//打印div id
接近
保存更改
服务器端(PHP) 假设您有一个函数来执行查询
,其中id=$id

public function query($id){
     //some code to execute the query and getting the result array
     //i can provide you an example, but i guess u have that figured out
     return $result;
}
关于结果,要在服务器端为div构建html,让我们假设您的表有三个字段
field\u 1、field\u 2、field\u 3
(非常原始)

//名称描述函数的功能
公共函数调用函数viaajaxtoGetMyDataintheModal(){
$theresult=查询($_POST['id']);
//当然,如果是空的或其他什么的,请验证
$html=“”;
//让我们创建一个简单的表
$html.=“”;//引导类
$html.=“字段1字段2字段2”;
foreach($theresult作为$item){
$html.=”;
$html.=''.$item['field1'].'';
//与字段_2和字段_3相同
$html.=”;
}
$html.=”;
echo$html;
}
如何使用Jquery和ajax调用它? 例如,您的信息图标如下(使用id)

$(文档).ready(函数(){
$(文档)。在('单击','获取我的数据图标')上,函数(e){
e、 预防默认值();
var uid=$(this.data('id');//这里是您的id
$('.modal body').html('');//清空模态体的html
$.ajax({
url:“../imCallingThisFunctionViaAjaxToGetMyDataInTheModal”,
键入:“POST”,
数据:{id:uid}
})
.完成(功能(数据){
console.log(数据);//如果你想。。。
$('.modal body').html(“+data+”);
//在模态体中打印结果
})
.fail(函数(){
//万一失败了
$('.modal body').html('重试!');
});
});
});

希望我的答案对您有所帮助。

您需要使用ajax和javascript在modal box上“写入”查询结果,并将发布完整的答案,但这需要一些时间…请稍候:-D@FranciscoHahn好的,谢谢你一个更详细的方法来做这件事,我发布的是我如何在一个应用程序中实现它,我删除了一些东西。
public function query($id){
     //some code to execute the query and getting the result array
     //i can provide you an example, but i guess u have that figured out
     return $result;
}
//The name describes what the function does
public function imCallingThisFunctionViaAjaxToGetMyDataInTheModal(){
     $theresult = query($_POST['id']);
     //of course validate if empty or whatever
     $html = "";
     //lets create a simple table
     $html.="<table class="table table-bordered">"; //bootstrap classes
     $html.="<thead><th>field_1</th><th>field_2</th><th>field_2</th></thead><tbody>";
     foreach($theresult as $item){
           $html.="<tr>";
           $html.="<td>".$item['field1']."</td>";
           //same with field_2, and field_3
           $html.="</tr>";
     }
     $html.="</tbody></table>";
     echo $html;
}
$(document).ready(function () {
$(document).on('click', '#get-my-data-icon', function (e) {
    e.preventDefault();
    var uid = $(this).data('id'); //here your get the id
    $('.modal-body').html('');    //empty the html of the modal body
    $.ajax({
        url: "../imCallingThisFunctionViaAjaxToGetMyDataInTheModal",
        type: 'POST',
        data: {id: uid}
    })
            .done(function (data) {
                console.log(data); //if u want...
                $('.modal-body').html("<div>"+data+"</div>"); 
                //print the result in the modal-body 
            })
            .fail(function () {
                //in case it fails
                $('.modal-body').html('<i class="fa fa-warning"></i> Try again!');
            });

});
});