Overview
this is a fast way to delete Records from your mysql datebase without loading the page .
this jquery code can Delete a any number of Records with animation fade-out effect using jQuery and Ajax.
this function is depending on
jQuery.post( url, data, callback, type )
but in our function we dont use the url becuase we use the same page url and use isset() PHP function to get the id.
Requirements
jQuery
Contents
- index.php
- db.php(Database configuration)
- jquery.js
db.php
<?php
mysql_select_db('users',mysql_connect('localhost','root',''));
?>
Database Table Code
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`) VALUES
(1, 'jooria'),
(2, 'Go'),
(3, 'Profeser');
index.php
<head>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="Jooria_Delete_0.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('.delete').Jooria_Delete('id','Sure you want to delete this one?');
});
</script>
</head>
<?php
include ('db.php');
if(isset($_POST['id'])){
mysql_query("delete from users where id='".intval($_POST['id'])."'");
exit;
}
$sql = mysql_query("select * from users order by id asc");
echo "<ul>";
while($Row = mysql_fetch_array($sql)){
extract($Row);
echo "<li>$name [<a id='$id' style='color:#ff0000;' class='delete' href='#'>Delete</a>]</li>n";
}
echo "</ul>";
?>
Category:
Website Programming | Views: 5,111 | on: September 22, 2009 | by:
jooria