php - 每次单击按钮然后将其用于查询时如何获取 php 变量?
问题描述
每次单击按钮时,我都会尝试获取 $row['username'] ,以便在查询中使用它。但只显示第一个数组。我想使用jquery,但我对此知之甚少。
<?php
$query = $con->query("SELECT * FROM account WHERE acc = 'agency'");
while($row = mysqli_fetch_array($query))
{
echo $row['username'];
?>
<button data-target="#select" data-toggle="modal">Select</button>
<?php } ?>
<div class="modal" id="select">
<?php
$query = $con->query("SELECT * FROM sample WHERE username = ".$row['username']."");
?>
解决方案
<button id="select"
class="selectUser"
data-username="<?php echo $row['username']; ?>"
data-toggle="modal"
data-target="#select">Select</button>
我拿了您在评论中添加的 html,并为其添加了一个类。现在你需要做的就是为它创建你的javascript绑定。
//find all the buttons with the selectUser class and bind a click handler to them
$('.selectUser').on('click', function(e){
//turn the element into a jQuery object so we can use jQuery methods
var $selectUser = $(this);
//log the username data attribute from the button
console.log($selectUser.data('username'));
//you could also get the attribute without jQuery
console.log(this.getAttribute('data-username'));
});
推荐阅读
- python - Django 消息未显示在联系表单模板上
- c++ - 在函数结束后使用指向变量的指针是否安全?
- generics - 使用泛型函数制作盒装特征会返回错误
- r - 如何根据两个条件从数据集中检索值并附加到另一个数据集
- windows - Windows Server - 有没有办法通过特定程序获取一段时间内的 CPU 使用情况?
- node.js - Intellisence 在 Node.js 的 Visual Studio 代码中不起作用
- java - 我的用于比较两个区域 id 时间的 java 程序显示错误
- c++ - 我可以使用一个类来控制 C++ 中另一个类的对象吗?(阿杜诺)
- mysql - mysql-docker-container - ERROR 1045 (28000): Access denied for user 'root'@'localhost' (使用密码: NO)
- activerecord - Rails 6 子类不会自动创建自己的类型