2016-07-05 89 views
0

我试图从PHP查询填充的表中传递一个变量。将PHP变量传递给表中的JavaScript函数

在表的每一行上有三个按钮。一个是删除按钮,如果点击它将运行一个JavaScript警报,它将一个变量传递给另一个PHP页面,并从数据库中删除该页面。问题是当按钮被点击时,传递的变量始终是表格最后一行的变量。不是被点击的行。

代码:

<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> 
+0

如果你使用jQuery,你可以填充一个'data'字段并在你的按钮上使用它来触发一个特殊的链接。这通常比管理每个按钮更容易管理。 – tadman

+0

onclick =“myFunction(<?php echo $ row [”recordnumber“];?>)”get tha value in function and set js itself function myFunction(recordnum){ –

+0

你明白了吗? @劳伦斯辣椒 –

回答

2

while($row = mysqli_fetch_assoc($result))循环声明javascript函数myFunction多次,都具有相同的名称。像这样

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

函数不能具有相同的名称。最简单的解决方案是使用不同的名称。

<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> 

更好,也简单的解决方案是使用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> 

当你把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> 
+0

是的!谢谢你的作品! –

0

答案很简单,你可以为你的表的所有元素调用函数myFunction()。

然后当你调用myFunction()时必须执行哪一个? Javascript决定执行最后一个。

您必须通过在每个元素上使用data-id来重构此代码,并调用一个使用此id的函数。