Search code examples
jqueryeventsjquery-eventspreventdefault

jQuery is not showing results


I need to make a jQuery to alert "are you sure you want to delete" when user click on delete. I've been trying a LOT but didn't find any solution.

This is my deletemovie.php

<?php
require('../system/initialize.php'); //load the connection file
?>

<html><script src="../js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $(".delete").click(function () {
            if ( !confirm('Are you sure you want to delete?') ) {
                return false;
            } else {
                $.ajax({
                    url: "deletemovie.php",
                    data: {movie_id},
                    type: 'get',
                    async: false,
                    success: function(result) {
                        if( result.success ) {
                            alert("You have successfully deleted");
                        }
                    }
                })
            }
        })
     )
</script>
</html>

<?php
$movie_id=$_GET['movie_id'];
$insert_user=mysql_query("DELETE  FROM `movie` WHERE `movie_id` ={$movie_id}");
if($insert_user)
{ echo "DELETED";}
else
{ echo "error in registration".mysql_error(); }
?>

Of course, in my list page I did:

  echo "<a class='delete' href='deletemovie.php?movie_id={$movie_id}'>". delete .'</a><br/></td>';

Solution

  • You are missing some semicolons and brackets. And also there should be a correction in the data parameter of ajax call. just use following code and it will work for you

    http://jsfiddle.net/n7v5w/

    $(function () {
        $(".delete").click(function () {
            if (!confirm('Are you sure you want to delete?')) {
                return false;
            } else {
                $.ajax({
                    url: "deletemovie.php",
                    data: {
                        'movie_id': <id of the movie>
                    },
                    type: 'get',
                    async: false,
                    success: function (result) {
                        if (result.success) {
                            alert("You have successfully deleted");
                        }
                    }
                });
            }
        });
    });
    

    Note: you have to replace the <id of the movie> with the actual id of the movie you want to delete