Reputation: 37
i am unable to delete records from database, fetching code is working but delete code is not working. please help me out.. thanks in advance
code to fetch data with ajax
$(document).ready(function(){
done();
});
function done(){
setTimeout(function(){
updates();
done();
}, 200);
}
function updates(){
$.getJSON("fetch.php",function(data){
$("table").empty();
$("table").append("<tr><td>Name</td><td>Date</td><td>Delete</td></tr>");
$.each(data.result, function(){
$("table").append("<tr><td>"+this['text']+"</td><td>"+this['date']+"</td><td><a id='del' href='"+this['id']+"'>Del</a></td></tr>");
});
});
}
code to delete data with ajax
$(function() {
$("#del").click(function(){
var element = $(this);
var id = element.attr("id");
var dataString = 'id=' + id;
if(confirm("Sure you want to delete this comment?"))
{
$.ajax({
type: "GET",
url: "del.php",
data: dataString,
success: function(){
}
});
}
return false;
});
});
php code del.php
$last_page_id = $_REQUEST['d_i_d'];
$sql = mysql_query("delete from time where id = '{$last_page_id}'");
if(!$sql){
echo mysql_error();
}else{
header('location: index.php');
}
Upvotes: 0
Views: 235
Reputation: 309
Try to pass data like this:
$.ajax{
url: 'del.php',
method: 'GET',
data: {
id: // Put your ID value here
}
success: function(){
// Put your success code here
}
}
Look that fragment:
$("#del").click(function(){
var element = $(this);
var id = element.attr("id");
...
id
variable will always hold "del" value, because you get ID attr of $('#del').
Upvotes: 0
Reputation: 2600
Ajax data: dataString = 'id=' + id;
calling it in php: $last_page_id = $_REQUEST['d_i_d'];
You can get the id with $_REQUEST['id']
Please note that mysql_ is deprecated: Why shouldn't I use mysql_* functions in PHP?
And that your code is open to SQL injection: http://en.wikipedia.org/wiki/SQL_injection
Upvotes: 1