-1

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>';
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ouzoumzak
  • 95
  • 1
  • 7
  • 1
    After reformatting your code, it looks like you are missing a close `}` for your main wrapper function. – Jeff B May 02 '13 at 20:16
  • 3
    If I had to guess, you copied this code from somewhere and are relying on the kindness of strangers to bail you out. You didn't pass an argument in your ajax data, your code formatting is mess, and a dozen other reasons this won't work. I'm not typically a jerk like this, but the only constructive answer I can supply is learn the code and start over. – Plummer May 02 '13 at 20:23

2 Answers2

1

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

zelibobla
  • 1,498
  • 1
  • 16
  • 23
0

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

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • 3
    Did you read the code? ``. -1 – tckmn May 02 '13 at 20:12
  • 1
    @ouzoumzak: thanks for the accepting and hope you will vote for the answer – Chamika Sandamal May 02 '13 at 20:24
  • 1
    and deleting every time 'movie_id': 1 :) ? – voodoo417 May 02 '13 at 20:29
  • @voodoo417: this is a sample code right. he can change the value with the any of variable. – Chamika Sandamal May 02 '13 at 20:32
  • @zeliboba: How can you say this is a bad sample? – Chamika Sandamal May 03 '13 at 05:18
  • From question we can see author don't know how to retrieve movie_id from his HTML. Your answer didn't help him in this nor before your edit not after. – zelibobla May 03 '13 at 07:06
  • if you read the question, retrieving `the movie_id` is not the question he asked. and this answer provide the answer for his question and he accepted that it is working for his scenario. remember this is not a code review site. so simply he wanted to get alert work not delete function work. – Chamika Sandamal May 03 '13 at 07:36
  • But for some reason you gave him an answer about `movie_id`. A part of your post about confirmation is not full and will work from time to time (you change nothing from code of topic starter here) because you didn't stop default event handling. In resume, you give bad reply. I can't realize why topic starter accepted your suggestion. Maybe should be because his knowledges are too low. – zelibobla May 03 '13 at 13:19