如何在不更改显示的屏幕数据的情况下更新表


How to update a table without changing the onscreen data displayed

我需要显示在MySQL表上的数百个项目的简单列表,查看屏幕上的列表并单击每个不需要的项目旁边的链接以从表中删除它。这是一项内部管理程序;没有外部用户参与。我不需要这个项目立刻从列表中消失;我将定期刷新列表,以便从表中删除的项不再列出。我不需要任何消息来确认项目已被删除。重要的是,我不想每次删除一个项目时都看不见列表,不得不点击"返回"按钮返回列表。

表使用MySQL。到目前为止,我所有的代码都是用PHP编写的。所以我使用php来显示项目列表,在非html屏幕上。以下是每一项的代码:

echo $item." <a href='item_delete.php?id=".$item."'>Delete item</a><br />";
下面是item_delete.php的代码:
<?php
require ('connect.php');    //  To define connection $con
$id = $_POST['id'];
mysqli_query($con, "DELETE FROM `items_table` WHERE `id` = $id");
?>

条目被正确删除,但显示空白屏幕(可以理解)。

我已经做了很多搜索,但大多数需要帮助的人想要做更高级的事情-因为我到目前为止已经设法避免学习JavaScript, jQuery和AJAX -我甚至无法确定我需要哪些技术来更新表而不改变屏幕。

我得到的印象是,每个PHP脚本总是带着"焦点",所以也许我需要一个小JavaScript脚本来做到这一点?如果是这样:-我可以只是改变item_delete.php item_delete.js或我必须定义非html列表作为html之一?- item_delete.js中需要哪些js代码?

我读过关于使用:header("location:javascript://history.go(-1)");或者:header('Location: ')。$ _SERVER [' HTTP_REFERER ']);但他们不会回到屏幕上的列表。

我不认为我想要js脚本执行一个虚拟的"回去",因为列表最初是通过使用(大约20)$_POST参数产生的,所以我似乎仍然必须每次刷新它。

所以我想要一个解决方案来保留列表-而不是离开它并返回它。谢谢。

使用javascript进行异步删除是很有意义的。然而,实现您想要的最简单和最混乱的方法是在链接中添加target="_blank"(这将为您执行的每个删除请求留下一个打开的空白选项卡)。

echo $item." <a href='item_delete.php?id=".$item."'       target='_blank'>Delete item</a><br />";

或者您可以通过在每个项目前面添加复选框来解决这个问题,选中您想要删除的项目并将它们作为表单参数提交给删除脚本

如果您想在实际PHP代码运行后删除屏幕表中的一行,您可以使用以下实现:

链接的HTML结构需要一个唯一的类名,例如:

echo '<a href="" onclick="return false" class="item-delete" data-item-id="' . $item . '">Delete item</a>'

注意,项目id存储在HTML5 data属性中。我还添加了一个onclick事件处理程序,它返回false,以避免链接刷新页面。

用于删除项目的javascript使用JQuery AJAX方法,绑定到指定的类,即:item-delete。实现需要Jquery版本>= 1.9.0

(function(){
    $('.item-delete').click(function(event) {
        var target = $(event.target);
        var id     = target.data('item-id');
        $.ajax({
           url: 'item_delete.php',
           method: 'POST',
           data: {
               id: id
           },
        }).done(function() {
            target.remove();
        }).error(function(err) {
            console.error('Could not delete item with ID: ' + id);
            console.error(err);
        });
    });    
}())

事件监听器定义在一个自执行函数中,该函数在页面加载完成时自动执行,并避免污染全局命名空间。

您可以在同一页面上直接删除项目,而无需移动到另一个页面通过超链接传递id,然后让它最终删除不想要的东西。看看这个,请告诉我这是不是你想要的。想要的:-)

 // connection 
 mysql_connect("host", "user", "password");
 mysql_select_db("your database name");

 // select all the items from table.
 $selectQuery = mysql_query("SELECT * FROM table_name" );
 // use while loop to list all the items...
 while( $row = mysql_fetch_array($selectQuery) )
 {
 // list the items as a hyperlink, passing their id through the URL.
 ?>  
    <a href ="?id=<?php echo $row["id"];?>"> <?php echo "delete " .              $row["item_name"];  ?>  </a>
 <?php       
 } 
 // Below is the code to delete the item.
 if( isset( $_GET["id"] ) )
  { 
    $itemId = $_GET["id"]; 
   // query to delete item
   $deleteQuery  = mysql_query("DELETE FROM table_name WHERE id = '$itemId'       "); 
      //-----------THE MOST IMPORTANT PART. >>>
      // redirect if delete is successfull.
     if( $deleteQuery )
      {    
         // reload the page to get the items minus the deleted one...
         // let's say your sript name is delete.php      
             header("Location:delete.php");
          } 
        } 
         ?>
        </code>