如何在FOREACH中使用Modal

时间:2016-05-14 08:17:53

标签: javascript php jquery html modal-dialog

我选择一个按钮时会弹出一个模态。此模式应填充用户的名称。

问题在于,当我选择按钮时,它只会填充第一个用户的详细信息,而不会填充表中的其他用户。我不明白为什么。如果有人能指出为什么我会有很大责任。

这是我的代码

<?php
    class afficherUser {
        function connection(){
            $con=mysql_connect('localhost','root','');
            mysql_select_db('UserTable');
        }

        function ShowUser(){
            $sql="SELECT*FROM users  LIMIT 3 ";
            $req= mysql_query($sql);

            while ($data=mysql_fetch_assoc($req)) {
                $tab[]=$data;
            }
            return $tab;
        }    
    }

    $data=new afficherUser();
    $data->connection();
    $result=$data->ShowUser();

?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">

</head>
<body>

<?php foreach ($result as $value):?>

<!-- Trigger/Open The Model -->
<button id="myBtn" class="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">

   <?php echo $value["user"]." ".$value["u_id"];?>


    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>


<?php endforeach; ?>

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

模态ID应该是唯一的,并且应该有一种方法来识别模态id以触发相关模态。

按钮元素,

<button id="myBtn" class="myBtn" onclick="ShowModal('myModal-<?= $value["u_id"]?>')"Open Modal</button>

然后是模态ID,

<div id="myModal-<?= $value["u_id"]; ?>" class="modal">

和Javascript

function ShowModal(id)
{
  var modal = document.getElementById(id);
  modal.style.display = "block";
}

答案 1 :(得分:0)

id应该是唯一的。但是在您的代码中,您无法为所有触发器生成一个id 所有模态都有一个id

以例如,

 <button id="myBtn-<?= $value["u_id"]; ?>" class="myBtn">Open Modal</button>

并在你的div中,

 <div id="myModal-<?= $value["u_id"]; ?>" class="modal">

并确保修改您的javascript。

答案 2 :(得分:0)

这是另一种解决方案,

  <?php $count = 0; ?>
  <?php foreach ($result as $value):?>
    <button id="myBtn" class="myBtn" data-toggle="modal" data-target="#myModal<?php echo $count; ?>">Open Modal</button>
    <div id="myModal<?php echo $count; ?>" class="modal fade" role="dialog">
      <div class="modal-content">
        <div class="modal-header">
          <span class="close">×</span>
          <h2>Modal Header</h2>
        </div>
        <div class="modal-body">
          <?php echo $value["user"]." ".$value["u_id"];?>
        </div>
        <div class="modal-footer">
          <h3>Modal Footer</h3>
        </div>
      </div>
    </div>
    <?php $count++; ?>
  <?php endforeach; ?>

注意这一部分?

data-toggle="modal" data-target="#myModal<?php echo $count; ?>"

它将确定目标模态,#myModal<?php echo $count; ?>

#myModal0, 
#myModal1, 
#myModal2, 
浏览器中的

检查元素。

与此类似,

<div id="myModal<?php echo $count; ?>" class="modal fade" role="dialog">

这个div的id看起来和上面一样。这样做的原因是,你将在整个循环中拥有一个独特的ID。