Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在每次单击按钮时获取php变量,然后将其用于查询?_Php_Jquery_Html - Fatal编程技术网

如何在每次单击按钮时获取php变量,然后将其用于查询?

如何在每次单击按钮时获取php变量,然后将其用于查询?,php,jquery,html,Php,Jquery,Html,每次单击按钮时,我都试图获取$row['username'],以便在查询中使用它。但仅显示第一个数组。 我想使用jquery,但我对它知之甚少 <?php $query = $con->query("SELECT * FROM account WHERE acc = 'agency'"); while($row = mysqli_fetch_array($query)) { echo $row['username']; ?> <button

每次单击按钮时,我都试图获取$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']."");

  ?>

您可以将用户名作为数据字段放在按钮上,类似于您作为数据属性执行目标和切换的方式。同时在按钮上放置一个类。然后,您可以使用jQuery将单击处理程序绑定到按钮,通过类查找它们,然后您可以从按钮中获取数据属性<代码>$row
未在while循环之外定义,因此不能在第二个查询中使用它。您还应该了解服务器端和客户端语言之间的差异。PHP是服务器端,在发送到客户端浏览器之前,所有PHP都已执行。浏览器加载完服务器发送的内容后,JavaScript才会执行。@像这样吗@HarlyJohn yep,这会将其添加为数据属性。然后,您只需要使用javascript在其上绑定一个单击事件处理程序。@Taplar:我应该在jquery中做什么?我的查询中的$row['username']仍然设置第一个数组。我不确定您在说什么。如果您问的是一个与php查询返回的内容相关的问题,那么您问的是两个不同的问题;php查询以及如何使javascript提醒用户。如果是这样的话,那么这个问题太宽泛了,你需要把问题分成更小的部分。我的意思是,如果我单击包含当前数组的按钮。我想使用该数组,然后将其用作查询中的条件,例如从示例中选择*,其中username=“.$row['username']”。哦,你不能这样做。如注释中所述,PHP在生成返回给客户机的页面之前在服务器上运行。然后在客户端上运行javascript。javascript不能直接调用php。他们在不同的机器上操作。您必须执行从客户端到服务器的web请求才能执行更多php。
<button id="select"
        class="selectUser"
        data-username="<?php echo $row['username']; ?>"
        data-toggle="modal"
        data-target="#select">Select</button>
//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'));
});