0

I use ajax to show the database thusly:

while($row2 = mysql_fetch_assoc($result2))
            {
                echo "<div class=\"text\"><span>";
                echo $row2['name'];
                echo "</span><br/>";
            }
            echo "<b>";
            echo $row['subject'];
            echo "</b>";
            echo "<br>";
            echo $row['description'];
            echo "<div class=\"date\">" . date("D, M y g:ia",strtotime($row['timestamp'])) . "</div>";
            echo "</div>";
            echo "<div class=\"clear\"></div>";
            echo "<a href=\"?delete=";
            echo $row['id'];
            echo "\" class=\"delete\">Delete</a>";
            echo "</div>";

but I'm having trouble deleting it.

I've tried putting this in the head, but it doesn't actually delete it...:

    <script type="text/javascript">
$(document).ready(function() {
  $('a.delete').click(function(e) {
    e.preventDefault();
    var parent = $(this).parent();
    $.ajax({
      type: 'get',
      url: 'delete.php',
      data: 'ajax=1&delete=' + parent.attr('id').replace('record-',''), beforeSend: function() {
        parent.animate({'backgroundColor':'#fb6c6c'},300);
      }, success: function() {
            parent.slideUp(300,function() {
                parent.remove();
            });
      }
    });
  });
});
</script>

Any advice where to start?

6
  • Have you separately tested delete.php and know that it works when the correct query string is present? Have you used your browser's developer tools to see if a request is being made when the link is clicked, and whether that request is to the URL you expect? Commented Jul 25, 2011 at 9:04
  • You seem to be filling in the delete= parameter of your URL using an ID (parent-#) of some element that doesn't exist in the code you've shared for this webpage. Commented Jul 25, 2011 at 9:05
  • can you post the contents of delete.php to check where the problem occurs? Commented Jul 25, 2011 at 9:07
  • $id = $_POST['id']; $sql = "DELETE FROM ajax_demo WHERE id = $id"; mysql_query($sql) or die(mysql_error()); echo "Message Deleted."; Commented Jul 25, 2011 at 9:13
  • I'm getting a "$ is not defined" for this line: $('a.delete').click(function(e) { Commented Jul 25, 2011 at 9:16

1 Answer 1

1

Judging from the error you mentioned, I assume, you need to include JQuery before the javascript in the html head...

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

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.