Why my page doesnt refresh after deleting a row

时间:2018-02-01 18:23:27

标签: php

My page doesnt refresh after clicking the button delete

$connect = mysqli_connect("localhost", "root", "", "redcrossqc");
$sql = "DELETE FROM volunteers WHERE volunteer_id = '".$_POST["volunteer_id"]."'";  
if(mysqli_query($connect, $sql))  
{  
  echo 'Data Deleted';  
  echo "<meta http-equiv='refresh' content='0'>";
} 

meta http equiv = refresh doesnt seem to refresh.

heres the delete button:

<input type="button" name="delete" value="Delete" id="<?php echo $row["volunteer_id"]; ?>" class="btn btn-danger btn-sm delete_data"/>

script:

$(document).on('click', '.delete_data', function(){  
       var volunteer_id = $(this).attr("id");  
       $.ajax({  
            url:"delete.php",  
            method:"POST",  
            data:{volunteer_id:volunteer_id},   
            success:function(data){      
                 $('#volunteer_id').val(data.volunteer_id); 
            }  
       });  
  });  

1 个答案:

答案 0 :(得分:0)

正如在一些评论中所说的那样,meta标签只有在将它们放入HTML时才能使用。但是在ajax请求中meta标记不是执行,浏览器不会将响应视为一个普通的旧响应。

我已经更新了一些你的代码。我主要是按照评论中的说法进行的。

正如您在评论中看到的那样,您的delete.php文件是SQL注入的安全漏洞。您应该使用prepared statement

<?php
//delete.php
$connect = mysqli_connect("localhost", "root", "", "redcrossqc");
/*
 * You SHOULD use a prepared statement here, you could be victim of
 * SQL injection.
 */
$sql = "DELETE FROM volunteers WHERE volunteer_id = '".$_POST["volunteer_id"]."'";  
mysqli_query($connect, $sql); //Delete but do nothing then.

这是您的主页,几乎所有内容都相同,但在success页面使用window.location.reload()重新加载后

//yourpage.html
$(document).on('click', '.delete_data', function(){
   var volunteer_id = $(this).attr("id");
   $.ajax({
        url:"delete.php",
        method:"POST",
        data:{volunteer_id:volunteer_id},
        success:function(data){
             $('#volunteer_id').val(data.volunteer_id);
             window.location.reload(); //Reload the page on success
        }
   });

});

相关问题