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

时间:2018-08-07 15:06:44

标签: 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 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']."");

  ?>

1 个答案:

答案 0 :(得分:0)

<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'));
});