0

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');
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Mani
  • 37
  • 5

2 Answers2

1

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

Community
  • 1
  • 1
nvanesch
  • 2,540
  • 14
  • 25
  • 1
    I said that [`5 minutes prior to this`](http://stackoverflow.com/questions/26465335/delete-data-with-ajax#comment41568714_26465335) waiting on OP's response. – Funk Forty Niner Oct 20 '14 at 12:18
  • 2
    [`Now you see why`](http://stackoverflow.com/questions/26465335/delete-data-with-ajax#comment41568982_26465335) I use comments first to make sure. – Funk Forty Niner Oct 20 '14 at 12:27
  • Lol, got a point there. Noticing the obvious mistake made me overlook the other error. However, It just took me 5 min to get the links in the article (had to look them up) so basically we were answering the same answer only through a different method, I was not copying your comment. – nvanesch Oct 20 '14 at 12:33
  • Not a problem. Whenever I feel that something may not be clear or using a comment may only partially solve a problem, I will use the commments box first, just to be certain. *Cheers* – Funk Forty Niner Oct 20 '14 at 12:40
0

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').

rgwsk65
  • 199
  • 2
  • 16
  • sir my this line of code is ok.... please guide me Del – Mani Oct 20 '14 at 12:31
  • The [docs](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) sais something different... `Type: PlainObject or String or Array` I don't say it's wrong but it is not the issue :) – VDP Oct 20 '14 at 12:32
  • Passing as object or string is not the issue, but the part about alweays sending "del" value is true. @Mani on that html you should use `.attr('href')` – nvanesch Oct 20 '14 at 12:43
  • `$('#del').click(function(){` `var target = $(this).attr('href');` `...` Now you can pass `target` variable as `id` in `data`. – rgwsk65 Oct 20 '14 at 16:52