Wednesday, 8 October 2014

PHP Delete Records From Database Using jQuery

 
 
 
index.php 
 
 
<?php
$connection = mysql_connect('localhost', 'root', '12345');
$db = mysql_select_db('school', $connection);
?>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
#container {
width: 500px;
margin: auto;
border: 1px dotted;

}
#header {
Width: 550px;
margin: auto;
}
#footer {
width 600px;

margin: 100px 400px;
}
</style>
<script type="text/javascript">
$('document').ready(function(){
$('a').click(function(){
var del_id = $(this).attr('id');
var parent = $(this).parent();
$.post('delete.php', {id:del_id},function(data){
parent.slideUp('slow', function() {$(this).remove();});
});
});
});
</script>
</head>
<body>
<div id="header">
<h2> Delete records from database using jQuery- InfoTuts </h2>
</div>
<div id="container">
<?php
$query = mysql_query("select * from class order by id desc LIMIT 10", $connection);
echo "<ul>";
while($row = mysql_fetch_assoc($query)){
echo "<li>{$row['name']}<a href=\"javascript:return(0);\" id=\"{$row['id']}\"><img src=\"remove.png\" height=\"12px\" width=\"12px\"/></a></li>";
}
echo "</ul>";
?>
</div>
</body>
</html>
<?php mysql_close($connection); ?>
--------------------------------------------------------------------------------
 
delete.php: 
 
<?php
$connection = mysql_connect('localhost', 'root', '12345');
$db = mysql_select_db('school', $connection);
$id = $_POST['id'];
$safeid = mysql_real_escape_string($id);
$query = mysql_query("delete from class where id=$safeid", $connection);
if(mysql_affected_rows()>0){   // Just for testing
echo "Success";
}else{
echo "Error deleting Data";
}

?> 

0 comments:

Post a Comment