2016-07-27 86 views
0

我使用甜蜜的提醒对话框进行删除确认。删除前的甜蜜警报确认

如果用户点击“是” - >运行SQL删除。

如果用户点击“否” - >什么也不做,请保留记录。

<?php 
if(isset($_POST['button_delete'])) { 

echo '<script type="text/javascript">'; 
echo 'setTimeout(function(){swal({ 
     title: "Are you sure?", 
     text:"You will not be able to recover this course!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     cancelButtonText: "No, cancel please!", 
     closeOnConfirm: false, 
     closeOnCancel: false}, 
     function(isConfirm) 
     { 
      if(isConfirm) 
      { 
       swal("Deleted!", "Your file has been deleted.", "success");'; 

     $deleteCourse = mysqli_real_escape_string($conn, $_POST['button_delete']); 
     $user = mysqli_real_escape_string($conn, $_SESSION['session_user']); 

     mysqli_query($conn, "DELETE FROM enroll 
          WHERE CourseCode LIKE '$deleteCourse' 
          AND UserID=$user"); 

echo ' 
      } 
      else 
      { 
       swal("Cancelled", "Your file is safe.", "error"); 
      }})}, 100);'; 
echo '</script>'; 

?> 

不管我点击“是”或“否”,SQL仍然会运行并删除记录。点击是/否时如何使它做不同的功能?

谢谢你的帮助。

+0

[客户端和服务器端编程有什么区别?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and -server-side-programming) – Epodax

+0

请分割你的代码并使用ajax。 –

回答

0

这是因为无论如何PHP代码都会被执行。 PHP不检查它是否在JavaScript if内部。你必须在PHP内部进行检查。

一种方法是在确认按钮被按下后让按钮发出AJAX请求。另一种是在点击时使用简单的POST(我认为你不需要,因此AJAX会更好)。