1

I have a form that can delete records from a MySql database using ajax and jQuery. I'm trying to get the jQuery to only select the relevant record passed to it and not just delete the top row of records which it does at the moment. I think I need to get

<div class="'.$id.'">

from my form and make it a data attribute that can be selected. Hope I'm making myself clear. Many thanks.

<script type="text/javascript">
    //Delete Review
    $(document).ready(function(){
        //Need to get $id here.
        $(".deleteReview").click(function (e) {
            e.preventDefault();
            var username = $("#username").val();
            var film_id = $("#film_id").val();
            var id = $("#id").val();

            $.post('ajax_deleteReview.php', {
                username: username,     
                film_id: film_id, 
                id: id
            }, function(data) {
                $("#message").html(data);
                $("#message").hide();
                $("#message").fadeIn(500); 
                $("#message").fadeOut(2500); 
            });
            return false;
        });
    });
</script>
<form>
    <div class="'.$id.'">
        <input type="hidden" id="username" name="username" value="'. $username.'">
        <input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
        <input type="hidden" id="id" name="id" value="'.$id .'">
        <button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
    </form>
</div>
5
  • 2
    Where is deleteReview element? Wild guess, you need $(this).closest('div').attr('class') Commented Dec 29, 2015 at 12:31
  • <div class ="'.$id.'"> was the deleteReview element. Commented Dec 29, 2015 at 12:35
  • why not give <div id ="<?php $id; ?>"? Commented Dec 29, 2015 at 12:36
  • 2
    $(".deleteReview"). will never fire with this code. @Dray That would be invalid if $id is an integer; ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").. w3.org/TR/html4/types.html#type-id Commented Dec 29, 2015 at 12:41
  • oh! i totally forgot! thanks buddy. @chris85 Commented Dec 29, 2015 at 12:44

2 Answers 2

3

I would recommend you to use data-* prefixed attributes to persists your data.

HTML

 <div data-id="'.$id.'" data-username="'. $username.'" data-filmid="'.$film_id .'" class="deleteReview">
    <button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
 <div >

Then you can fetch it using .data(key)

Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute

Script

 //Delete Review
$(document).ready(function(){
    //Need to get $id here.
    $(".deleteReview").click(function (e) {
        e.preventDefault();
        var username = $(this).data("username");
        var film_id = $(this).data('filmid');
        var id = $(this).data('id');

        ......
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

First in your PHP do something like this, you add the data attributes to your delete button since you are not actually submitting a real form. I also moved the deleteReview class to the button.

print '<button type="submit" class="deleteReview" data-id="'.$id.'" data-film-id="'.$film_id.'" data-username="'.htmlentities($username).'" class="btn btn-danger btn-xs pull-right">delete</button>';

Then, in your jQuery, update click event callback like so:

$(".deleteReview").click(function (e) {
    e.preventDefault();
    var username=$(this).data('username');
    var film_id=$(this).data("film-id");
    var id=$(this).data('id');
    /* the rest of your code */

The data attributes become accessible easily with jQuery using $(this).data('some-attribute-name')

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.