0

My delete button:

$('#taglist li #delete').bind('click', function(){
        pic_id = $(this).parent().attr("rel");
        $.post('savetag.php', {"type":"remove", pic_id:pic_id}, function(data){
            viewtag();
          });
    });

savetag.php

if ($_POST['type'] == "remove")
{
    $pic_id = $_POST['pic_id'];

    $sql = "DELETE FROM image_tag WHERE `pic_id` = $pic_id";
    mysql_query($sql);
}

Here is the script that views the tags, viewtag.php:

$sql = "SELECT * FROM image_tag ORDER BY `pic_id`";
$qry = mysql_query($sql);
$rs = mysql_fetch_array($qry);

if ($rs){
  do{
    echo '<li rel="'.$rs['pic_id'].'"><a>'.$rs['name'].'</a> <a class = "delete">Delete</a></li>';
  }while($rs = mysql_fetch_array($qry));
}

Here is the function that allows viewtag.php into my index.php:

viewtag();

    function viewtag()
    {
     $.post('viewtag.php', function(data){
            $('#taglist ol').html(data);
          });
    }

So what is the problem, why is it not deleting the table? Also the anchor tag isn't working (anchor tag which is echoed from viewtag.php). What is the problem? Thanks.

3
  • are you getting ajax response? just echo "hi"; before $pic_id in your savetag.php and checkout. Commented Feb 6, 2013 at 6:24
  • echo $pic_id in your savetag.php file and check that its display ID or not ? Commented Feb 6, 2013 at 6:25
  • Open the dev tool bar or Firebug (if using Firefox) and begin to look at the Ajax request (did it contain all parameters ?). Secondly, add a PHP echo in your savetag.php to check the SQL request. In other words..debug. You can also give us your HTML code, it can help Commented Feb 6, 2013 at 6:25

2 Answers 2

1

what i could see is .. your <a> tag isn't working since the tag is added dynamically so you need to use on delegate event..and notice your <a> has a class but your selector is id.. use .delete as a selector

$('#taglist').on('click','a.delete', function(){  //u can use #taglist li a.delete too
    pic_id = $(this).parent().attr("rel");
    $.post('savetag.php', {"type":"remove", pic_id:pic_id}, function(data){
        viewtag();
      });
});

you can go through the link if you want to read more about on event

Sign up to request clarification or add additional context in comments.

1 Comment

welocme... happy coding.. let me know if any problem ... :)
0

First, you're selecting for ID, but you have a class:

$('#taglist li #delete').

should be:

$('.delete').bind('click', function(e){
        pic_id = $(this).parent('li').attr("rel");
        $.post('savetag.php', {"type":"remove", pic_id:pic_id}, function(data){
            viewtag();
          });
 e.preventDefault();
});

Your links won't work because they have no href tag, so how do the browser knows the right anchor?

  <?php while($rs = mysql_fetch_array($qry)): ?>
     <li rel="<?php echo $rs['pic_id'];?>"><a href="#<?php echo $rs['name'];?>"><?php echo $rs['name'];?></a> <a class="delete" href="#">Delete</a></li>
  <?php endwhile;?>

Also, I suspect there are issues in he viewtag() function too, but you didn't post the relative html so I can't help much on that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.