【问题标题】:POST or GET PHP values within while loop using modal使用模式在 while 循环中 POST 或 GET PHP 值
【发布时间】:2018-05-24 22:23:29
【问题描述】:

下面是用户的 mysqli 查询,这些查询显示在 while 循环表中,其中一列中的名字和另一列中的可点击链接。我想单击使用 javascript 或什至像 jquery 之类的其他东西的 OPEN 打开模态覆盖屏幕,然后单击将 POST 或将 PHP 值发送到同一页面或其他页面的消息按钮。下面的问题是只有第一个 $id_friend 以模态显示,这取决于它是在循环中还是在循环外。请帮助找到安全轻松地检索这些值的最佳方法。

<?php while($row = mysqli_fetch_array($result)) {  ?>

 $id_friend = $row['id_friend'];

 $firstName = $row['firstName'];


<table>
<tr>
<td><a><?php echo $firstName;  ?></a></td>
<td>

<!-- opens modal -->

<a href="#" id="<?php echo $row['friend_id'];?>" onclick="show(this.id)" >open </a>

<!-- modal -->

<div id="myNav2" class="overlay2">

<a href="javascript:void(0)" class="closebtn2" 
onclick="closeNav233()">&times;</a>

  <div class="overlay2-content">
     <div>
<form  action="getuser2.php" method="POST">
<button  name="friend_id" value="<?php echo $id_friend; ?>" 
type="submit">MESSAGE</button>
<input  class="friend_id" type="hidden" name="friend_id" value="<?php echo 
$id_friend; ?>" >
</form>
     </div>
  </div>
</div>

</td>
</tr>

<?php
 } //end of while loop
?> 

</table>

<script>
function show() {

document.getElementById("myNav2").style.width = "100%"; //opens modal

option = $(this).attr('id');
console.log(option ); //to check clicked id is Ok

$.ajax({
    url: 'testmodal.php',
    type: 'GET',
    data: {option : id},
    success: function(data) {
    alert($(this).data('id'));
    document.getElementById('myNav2').innerHTML =data;
    }
});

}

</script>

<style>
body {
font-family: 'Lato', sans-serif;
}

.overlay2 {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0, 0.9);
overflow-x: hidden;
transition: 0.5s;
}

.overlay2-content {
position: relative;
top: 25%;
width: 100%;
text-align: center;
margin-top: 30px;
}

.overlay2 a {
padding: 8px;
text-decoration: none;
font-size: 36px;
color: #818181;
display: block;
transition: 0.3s;
}

.overlay2 a:hover, .overlay2 a:focus {
color: #f1f1f1;
}

.overlay2 .closebtn2 {
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay2 a {font-size: 20px}
  .overlay2 .closebtn2 {
  font-size: 40px;
   top: 15px;
  right: 35px;
 }
 }
</style>

<script>

function closeNav233() {
document.getElementById("myNav2").style.width = "0%";
}
</script>

【问题讨论】:

    标签: jquery post mysqli while-loop modal-dialog


    【解决方案1】:
    while($row = mysqli_fetch_array($result))  
    {
    $row['post_id'].$row['post_title'].$row['content']; 
    
    }
    

    这就是在使用 first_name 和friend_id 时使用一些先前初始化的数据来获取一行中的列的方式

    【讨论】:

    • 谢谢,但无论我使用 $row['friend_id'] 还是像 $friend_id 之类的初始化数据,它都只会在模态叠加层中回显第一个或最后一个 $friend_id,具体取决于它是否在循环中。在带有表格的页面上工作正常。我想我需要 Ajax。
    • 我可以看一个这样的例子以便我理解,我觉得可能是我没有正确理解这个问题
    猜你喜欢
    • 2021-07-18
    • 2016-02-21
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多