2017-09-24 71 views
0

我使用php,html和sweetalert2 java脚本函数向用户显示一些通知。我需要在将用户重定向到同一(目标)页面后显示警报。这是我的代码有如何在php中执行一些java脚本后重定向用户

function delete_cat(){ 
     include("include/db.php"); 
     include("include/notification.php"); 

     $delete_cat_id = $_GET['delete_cat']; 
     $delete_cat= $con->prepare("DELETE FROM main_cat WHERE cat_id='$delete_cat_id'"); 

     if($delete_cat->execute()){ 
      echo '<script type="text/javascript"> 
       deleteMainCatSuccess(); 
       </script>'; 
      echo "<script>window.open('index.php?viewall_cat','_self')</script>"; 
     } 
     else{ 
      echo '<script type="text/javascript"> 
       deleteMainCatError(); 
       </script>'; 
     } 
    } 

这是通知Java脚本

<script type="text/javascript"> 
    function deleteMainCatSuccess(){ 
     swal({ 
       title: "Success", 
       text: "Delete Main Category Success", 
       type: "success", 
       timer: 3000 
     }, 
      function(){ 
       //event to perform on click of ok button of sweetalert 
     }); 
    } 
</script> 

这工作,但通知显示小于1秒,重定向页面后。我需要留出一些时间来显示通知,然后将用户重定向到我的目标页面。我在选项中包含了timer: 3000,但它似乎没有什么区别。

我能做些什么来解决这个问题?

+0

移动的'window.open'打电话到'swal'回调函数。 –

+0

我可以在哪里学习?你可以给参考:) –

+0

你的代码是非常危险的。您对SQL注入攻击持开放态度......使用准备/参数化查询完全避免此问题。您还正在对HTTP GET请求进行更改。这违反了各种标准,并且容易受到跨源攻击。有一个'DELETE'动词。如果您要删除某些内容,请使用它。对于数据更新,酌情使用'POST'或'PUT'。 – Brad

回答

0

我固定它像

<script type="text/javascript"> 
function deleteMainCatSuccess(){ 
    swal({ 
      title: "Success", 
      text: "Delete Main Category Success", 
      type: "success", 
      timer: 3000 
     }).then(function(){ 
      window.location.href='index.php?viewall_cat'; 
    }); 
} 
0
swal({ 
    title: "Success", 
    text: "Delete Main Category Success", 
    type: "success" 
}, 
function(){ 
    window.open('index.php?viewall_cat','_self'); 
}); 

是这样的?