简体   繁体   English

将PHP变量传递到表中的JavaScript函数

[英]Passing PHP variable to JavaScript function in a table

I am trying to pass a variable from a table that was populated by a PHP query. 我试图从PHP查询填充的表中传递变量。

On each row of the table there are three buttons. 表格的每一行上都有三个按钮。 One is a delete button that if clicked it will run a JavaScript alert which passes a variable to another PHP page where it is deleted from the database. 一个是删除按钮,如果单击该按钮,它将运行JavaScript警报,该警报将变量传递到另一个PHP页面,在该页面中将其从数据库中删除。 The problem is when the button is clicked the variable passed is always the variable from the last row of the table. 问题是当单击按钮时,传递的变量始终是表最后一行的变量。 Not the row being clicked. 不是正在单击的行。

The code: 编码:

<table class="table table-bordered table-striped js-dataTable-full-pagination">
<?php while($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
        <td class="text-center"><a href="/record.php?report=<?php echo $row['recordnumber']; ?>"><?php echo $row['recordnumber']; ?></a></td>
        <td class="font-w600"><?php echo $row["description"];?></td>
        <td class="hidden-xs"><?php echo $row["type"];?></td>
        <td class="hidden-xs"><?php echo $row["user"];?></td>
        <td class="hidden-xs"><?php echo $row["edate"];?></td>
        <td class="text-center">
            <a class="btn btn-xs btn-primary" href="/updaterecordform.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="Edit Record"><i class="fa fa-pencil"></i></a>
            <a class="btn btn-xs btn-danger" onclick="myFunction()" data-toggle="tooltip" title="Delete Prefix"><i class="fa fa-close"></i><script>
                function myFunction() {
                  swal({
                      title: 'Are you sure?',
                      text: 'You will not be able to recover this Record!',
                      type: 'warning',
                      showCancelButton: true,
                      confirmButtonColor: '#d26a5c',
                      confirmButtonText: 'Yes, delete it!',
                      closeOnConfirm: false,
                      html: false
                  }, function () {
                      window.location.href = 'deleterecord.php?id=<?php echo $row['recordnumber']; ?>'
                  });
                }
                </script></a>
            <a class="btn btn-xs btn-primary" href="/record.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="View Details"><i class="fa fa-list-ul"></i></a>
        </td>
    </tr>
<?php } ?>
</table>

The while($row = mysqli_fetch_assoc($result)) loop declares the javascript function myFunction a number of times, all with the same name. while($row = mysqli_fetch_assoc($result))循环多次声明javascript函数myFunction ,它们都具有相同的名称。 Like this 像这样

<?php while($row = mysqli_fetch_assoc($result)) { ?>
    <script>
    function myFunction() {} // this function name is repeated in the while loop
    <script>
<?php } ?>

Functions cannot have the same name. 函数不能具有相同的名称。 The simplest solution is to use different names. 最简单的解决方案是使用不同的名称。

<td class="text-center">
    <a class="btn btn-xs btn-primary" href="/updaterecordform.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="Edit Record"><i class="fa fa-pencil"></i></a>
    <a 
        class="btn btn-xs btn-danger" 
        onclick="myFunction<?php echo $row["recordnumber"];?>()" // <-- edited line
        data-toggle="tooltip" title="Delete Prefix"
    >
    <i class="fa fa-close"></i>
        <script>
            function myFunction<?php echo $row["recordnumber"];?>() { // <-- edited line
              swal({
                  title: 'Are you sure?',
                  text: 'You will not be able to recover this Record!',
                  type: 'warning',
                  showCancelButton: true,
                  confirmButtonColor: '#d26a5c',
                  confirmButtonText: 'Yes, delete it!',
                  closeOnConfirm: false,
                  html: false
              }, function () {
                  window.location.href = 'deleterecord.php?id=<?php echo $row['recordnumber']; ?>'
              });
            }
        </script></a>
    <a class="btn btn-xs btn-primary" href="/record.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="View Details"><i class="fa fa-list-ul"></i></a>
</td>

A better and also simple solution is to use recordnumber as function parameter 更好且简单的解决方案是将recordnumber用作函数参数

<td class="text-center">
    <a class="btn btn-xs btn-primary" href="/updaterecordform.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="Edit Record"><i class="fa fa-pencil"></i></a>
    <a 
        class="btn btn-xs btn-danger" 
        onclick="myFunction(<?php echo $row["recordnumber"];?>)" 
        data-toggle="tooltip" title="Delete Prefix"
    >
    <i class="fa fa-close"></i>
    <a class="btn btn-xs btn-primary" href="/record.php?id=<?php echo $row["recordnumber"];?>" data-toggle="tooltip" title="View Details"><i class="fa fa-list-ul"></i></a>
</td>

Where you put the javascript function outside the while loop so that it is not repeated 将javascript函数放在while循环之外的位置,这样就不会重复

<script>
function myFunction(recordnumber) {
  swal({
      title: 'Are you sure?',
      text: 'You will not be able to recover this Record!',
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#d26a5c',
      confirmButtonText: 'Yes, delete it!',
      closeOnConfirm: false,
      html: false
  }, function () {
      window.location.href = 'deleterecord.php?id=recordnumber';
  });
}
</script></a>

The answer is simple, you call you function myFunction() for ALL elements of your table. 答案很简单,您可以为表的所有元素调用函数myFunction()。

Then when you call myFunction() which one must be executed ? 然后,当您调用myFunction()时,必须执行哪一个? Javascript decide to execute the last one. Javascript决定执行最后一个。

You must refactor this code by using data-id on each element and call just ONE function that use this id. 您必须通过在每个元素上使用数据ID来重构此代码,并仅调用使用此ID的一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM