article

Friday, May 13, 2011

PHP Mysql jquery Delete Table Row With Confirmation like twitter message

PHP Mysql jquery Delete Table Row With Confirmation like twitter message

I am going to demonstrate a php mysql jqeury method I use to delete a row from mysql and then remove the html row with some nice jquery effects




dbconfig.php
//dbconfig.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "ednalan";
$mysql_database = "testdeleterowjquerymysql";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>


index.php
//index.php
<style type="text/css">
.top_row {
background-color: #CCCCCC;
color: #FFFFFF;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #9CF;
}
.manage_row {
background-color: #eeeeee;
color: #333;
text-align: center;
}
.message {
font-size: 24px;
padding: 5px;
text-align: center;
position: absolute;
left: 0px;
top: 0px;
right: 0px;
border-bottom-width: medium;
border-bottom-style: solid;
border-bottom-color: #F00;
background-color: #64943F;
display:none;
}
.manage_row td {
border-bottom-width: 1px;
border-left-width: 1px;
border-bottom-style: solid;
border-left-style: solid;
border-bottom-color: #CCC;
border-left-color: #CCC;
}
.top_row th {
padding: 5px;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #333;
font-weight: bolder;
border-left-width: 1px;
border-left-style: solid;
border-left-color: #333;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" >
function deleterow(id){
if (confirm('Are you sure want to delete?')) {
$.post('deleterow.php', {id: +id, ajax: 'true' },
function(){
$("#row_"+id).fadeOut("slow");
$(".message").fadeIn("slow");
$(".message").delay(2000).fadeOut(1000);
});
}
}
</script>
<div class="message">Row Deleted Successfully</div>

<table id="main_table" align="center" border="0" cellpadding="0" cellspacing="0" width="438" height="96">
<tbody>
<tr class="top_row">
<th scope="col" width="88">ID</th>
<th scope="col" width="293">Name</th>
<th scope="col" width="57">Delete</th>
</tr>
</tbody>
<?php
include("dbconfig.php");
$query4="select * from tblname order by id desc";
$result4 = mysql_query($query4);
$n = 0;
while($row=mysql_fetch_array($result4))
{
$n++;
$id=$row["Id"];
$name=$row["name"];
?>
<tbody>
<tr class="manage_row" id="row_<? echo $id; ?>">
<td><? echo $n; ?></td>
<td><? echo $name; ?></td>
<td id="delete">
<a href="#" onclick="deleterow(<? echo $id; ?>)">Delete</a>
</td>
</tr>
</tbody>
<? } ?>
</table>


deleterow.php
//deleterow.php
<?php
include("dbconfig.php");
$id=$_POST['id'];
$sql = "delete from lito_user where Id='$id'";
mysql_query( $sql);
?>

Related Post