简体   繁体   English

使用PHP删除没有刷新页面的记录

[英]Delete record without refresh page using php

I have HTML and PHP code as below: 我有如下的HTML和PHP代码:

HTML HTML

<a href="deleteData.php?id=<?php echo $value['id']?>"><button name="delte" class="btn">Delete</button></a>

deleteData.php deleteData.php

<?php
include ('include/connectdb.php');
$getid = $_GET['id'];
$sql = ("UPDATE tblworkfaire SET status=0 where id = ".$getid);
$res = mysql_query($sql) or die (mysql_error());

?>

This works great except that after the record is deleted the record is still displayed on the page until it is refreshed.How do I fix this.Anyone have any idea help me please.Thanks, 效果很好,除了删除记录后,记录仍会显示在页面上,直到刷新为止。如何解决此问题。有任何想法可以帮助我。谢谢,

Try Something like this --- 尝试这样的事情-

jQuery.ajax({
        type: "GET",
        url: deleteData.php,
        data:{'id':id},
        success:function(results)
        {   
                 .....
        }
        });

You should use Ajax . 您应该使用Ajax Try maybe with jQuery : 尝试使用jQuery
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

Then onSuccess method delete row using jQuery too. 然后onSuccess方法也使用jQuery删除行。

Here is the Complete Source Code for Delete Record without Refreshing the Page. 这是删除记录而不刷新页面的完整源代码。

Follow the Steps: 按照步骤:

Step1: 第1步:

DBConnect.php DBConnect.php

  class DBConnect
  {
    function DBConnect()
    {
       $link= mysql_connect("localhost","root","")or die("Local Host Error".mysql_error());
       mysql_select_db("test");
    }

    function viewData()
    {
      $query = "SELECT * FROM test_mysql";
      $resultset = mysql_query($query);
      return $resultset;
    }

    function DeleteData($userID)
    {
      $query = "DELETE FROM test_mysql WHERE id=".$userID;
      $resultset=mysql_query($query) ;
    }
  }

Step2: 第2步:

  delete.php 
   include_once'DBConnect.php';    
   if(isset($_REQUEST['userid']) && $_REQUEST['userid'])    
   {    
     $delObj= new DBConnect();    
     $delObj->DeleteData($_REQUEST['userid']);    
   }

Step 3: 第三步:

  index.php 
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function deletedata(id)
    {
      var xmlhttp;    
      if (id=="")
        {
            document.getElementById("Display").innerHTML="";
            return;
        }
      if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
      else
      {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
              window.location.reload()

            }
      }
          xmlhttp.open("GET","delete.php?userid="+id,true);
          xmlhttp.send();

    }
    </script>
    </head>
    <body>
    <?php 
    include 'DBConnect.php';
    $ViewObj= new DBConnect();
    $ResultSet=$ViewObj->viewData();
    ?>
    <span id ="Display">
    <table align="center" border="1" width="50%" cellpadding="4" cellspacing="4">
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>operation</th>
      <th align="center">Action</th>
    </tr>
    <?php
    while($row= mysql_fetch_array($ResultSet))
    {
    ?>
    <tr>
      <td><?php echo $row[0];?></td>
      <td><input type="text" name="txt"></td>
      <td><?php echo $row[1];?></td>
      <td align="center"><a href="#" onClick="deletedata('<?php echo $row[0];?>')" style="color:#00F"><b>Delete</b></a></td>
    </tr>
    <?php
    }
    ?>
    </table>
    </span>
    </body>
    </html>

if You Feel any problem then please let me know.hope it will help you. 如果您有任何问题,请告诉我。希望它将为您提供帮助。 Thank you. 谢谢。

使用redirect('my_original_page.php')它将重定向到您的主页,但应将其放置在执行删除查询之后

You have to use AJAX for this 您必须为此使用AJAX

use this tutorial 使用本教程

Deleting values from MySQL database with AJAX without page reload (edited) 使用AJAX从MySQL数据库删除值而无需重新加载页面(已编辑)

It will help you out. 它将帮助您。

Creaet Delete.php File Creaet Delete.php文件

 include_once'DBConnect.php';
 if(isset($_REQUEST['userid']) && $_REQUEST['userid'])
 {
 $delObj= new DBConnect();
 $delObj->DeleteData($_REQUEST['userid']);
 }

create DBConnect.php File 创建DBConnect.php文件

<?php

class DBConnect
{
function DBConnect()
{
    $link= mysql_connect("localhost","root","")or die("Local Host Error".mysql_error());
    mysql_select_db("mydb");

}

 function viewData()
{

    $query="select * from userinfo";
    $resultset=mysql_query($query) ;

    return $resultset;


}

function DeleteData($userID)
{

    $query="DELETE from userinfo where id=".$userID;

    $resultset=mysql_query($query) ;

}


}

?>

Create index.php file 创建index.php文件

<script>
function deletedata(id)
{
    var xmlhttp;    
    if (id=="")
      {
          document.getElementById("Display").innerHTML="";
          return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      }
    else
    {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            window.location.reload()

          }
    }
        xmlhttp.open("GET","delete.php?userid="+id,true);
        xmlhttp.send();

}
</script>    




<?php include 'DBConnect.php';
$ViewObj= new DBConnect();
$ResultSet=$ViewObj->viewData();?> // I have created one function which will get all the data from the database if you don't want to do this just call deletedata() function onClick event and pass Record ID as agrument which you want to delete on DELETE button.
<br /><br /><br />
<span id ="Display">
<table align="center" border="1">
<tr>
      <th>Name</th>
      <th>operation</th>
</tr>
<?php
while($row= mysql_fetch_array($ResultSet))
{?>


<tr>
    <td><input type="checkbox"></td>
    <td><?php echo $row[1];?></td>

    <td align="center"><a href="#" onclick="deletedata('<?php echo $row[0];?>')" style="color:#FF0000"><b>Delete</b></a>

    </td>
</tr>

<?php 


}
?>
</table>

I think this will help you :) 我认为这会对您有所帮助:)

Let me know if you are finding any problem. 如果您发现任何问题,请告诉我。

Either you can use php function header('location:yourPage.php'); 您可以使用php函数头文件('location:yourPage.php'); or do with the help of Ajax. 或在Ajax的帮助下完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM