Question

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>';
Was it helpful?

Solution 2

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

OTHER TIPS

I see at least 3 troubles in your code:

1.To make your confirmation rise up you need to stop usual handling of anchor click. This can be done like this:

$(".delete").click( function( event ) {
  event.preventDefault();
  if( !confirm( 'Are you sure you want to delete?' ) ){
    return false;

2.Next trouble you will meet, but you didn't describe here is a data you sent via ajax. It is specified incorrectly. To my mind in your case no need to specify data at all because all of it is already exist in your url (what means it will be sent as GET). All you need is to get href attribute of a link.

$(function () {
    $(".delete").click(function ( event ) {
        event.preventDefault();
        if (!confirm('Are you sure you want to delete?')) {
            return false;
        } else {
            var url = $( event.target ).attr( 'href' );
            $.ajax({
                url: url,
                async: false,
                success: function (result) {
                    if (result.success) {
                        alert("You have successfully deleted");
                    }
                }
            });
        }
    });
});

3.And the last trouble is nobody give you a job because nobody want to get such an unprotected code. You should never trust anything you receive from client. You can be sure, once somebody will use a hole you left and will kill your reputation. I mean this part of your code:

$movie_id=$_GET['movie_id'];
$insert_user=mysql_query("DELETE  FROM `movie` WHERE `movie_id` ={$movie_id}");

Something about risks I am talking about: http://en.wikipedia.org/wiki/Sql_injection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top