I have HTML and PHP code as below:
HTML
<a href="deleteData.php?id=<?php echo $value['id']?>"><button name="delte" class="btn">Delete</button></a>
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,
Creaet Delete.php File
include_once'DBConnect.php';
if(isset($_REQUEST['userid']) && $_REQUEST['userid'])
{
$delObj= new DBConnect();
$delObj->DeleteData($_REQUEST['userid']);
}
create DBConnect.php File
<?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
<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.