你如何阻止php脚本执行其他脚本?

时间:2018-02-08 19:33:22

标签: javascript php jquery

大家好,我的代码有4部分

  • 2个html按钮(激活用户,删除用户)
  • 一个html表
  • jquery关于在html表上获取user_id
  • php脚本执行更新或删除sql命令

情景:

我有一个html表(使用php脚本从SQL DB中提取数据)我可以选择表上的行并完美地提取他们的user_id,但问题是我有这两个按钮(激活用户,删除用户)一切正常,直到你把他们的代码放在一起问题是激活php脚本接受命令而不是删除用户脚本。他们是一个"休息;"在PHP脚本上就像切换案例一样,它不会执行另一个,而是专门为这两个按钮执行脚本。


这些是我的按钮

<a type="button" id="activateUser"> Activate Selected User</a> 
<a type="button" id="deleteUser"> Delete Selected User</a> 

这是用于突出显示我的html表格行的jQuery脚本

<script>
     $('tr').click(function () 
     {
         $('tr').removeClass('selected');
         $(this).addClass('selected');
         selectedRow = $(this);

     });

这是用于获取我的按钮的id并在激活所选用户按钮上获取user_id的jQuery脚本。

$("#activateUser").click(function () 
{
   var td = $(selectedRow).children('td');
   for (var i = 0; i < 1; ++i) {
   window.location.href = window.location.href+'?id='+td[i].innerText;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                       
});

这是用于获取我的按钮的ID并在删除所选用户按钮上获取user_id的jQuery脚本。

$("#deleteUser").click(function () 
{
   var td = $(selectedRow).children('td');
   for (var i = 0; i < 1; ++i) {
   window.location.href = window.location.href+'?id='+td[i].innerText;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                       
});

这是 PHP SCRIPT ,用于通过将user_active更改为1来激活用户,激活用户

  <?php
      $id = $_GET['id']; //gets the user_id from as GET request 
      $btnQuery  = "SELECT * FROM users WHERE user_id='$id'";

      $buttonquery = mysqli_query($DBLink, $btnQuery);
      $Actcount = mysqli_num_rows($buttonquery);

      $act = 1;

      if($Actcount == 1)
      {


         $sqlact = "UPDATE users SET user_active='$act' WHERE 
         user_id='$id'";
         $activate = mysqli_query($DBLink, $sqlact);


         if($activate)
         {
            echo $ActivatedUser = "<b>Success!</b> User successfully Activated.";   
    echo "<script>setTimeout(function()
    {
      window.location.replace('http://localhost/webpage/manage-user.php');                                      
   }, 1500);    
    </script>";                                                                                                                                                                                                                                                                     
         }                                                                                                                                                                                                                                                      
      }                                                                                                                                                                                                                                             
   ?>

这是删除所选用户的 PHP SCRIPT

<?php
    $id = $_GET['id'];
    $btnQuery  = "SELECT * FROM users WHERE user_id='$id'";

    $buttonquery = mysqli_query($DBLink, $btnQuery);
    delcount = mysqli_num_rows($buttonquery);

    if($delcount == 1)
    {
    $sqldel = "DELETE FROM users WHERE user_id='$id'";
    $delete = mysqli_query($DBLink, $sqldel);

    if($delete)
    {
         echo $Deleted = "<b>Success!</b> User successfully Deleted.";
         echo "<script>setTimeout(function()
         {                                                                                                                                                                                                                  
            window.location.replace('http://localhost/webpage/manage-user.php');                                        
         }, 1500);</script>";                                                                                                                                                                                                                                           
    }
}
?>

代码结构
[激活用户]按钮
用于在HTML表中选择行的脚本
用于获取用户激活的user_id的脚本
PHP PHP SCRIPT ON ACTIVATING

[删除用户]按钮
用于在HTML表中选择行的脚本
用于获取用户删除的user_id的脚本
PHP SCRIPT ON DELETING

同样,它在没有另一半的情况下运行良好,反之亦然,但当我尝试将它们组合时,它永远不会执行,它甚至只执行第一部分或有时执行错误。我尝试在每个php脚本的末尾使用exit()或die(),但这最糟糕。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

您需要在网址中添加一个参数,指明您需要的操作:

$("#activateUser").click(function () 
    {
       var td = $(selectedRow).children('td').first();
       window.location.href = window.location.href+'?action=activate&id='+$(td).text();
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                       
});
$("#deleteUser").click(function () 
    {
       var td = $(selectedRow).children('td').first();
       window.location.href = window.location.href+'?action=delete&id='+$(td).text();
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                       
});

然后在PHP脚本中检查$_GET['action']

if ($_GET['action'] == 'activate') {
    // code for activating account
} elseif ($_GET['action'] == 'delete' {
    // code for deleting account
} else {
    die("Invalid action");
}