jax
jax

Reputation: 57

How to pass database id in jquery

i want to delete a record that are showing in while loop from the database but before deleting i want to display a confirmation box.The code i have written is below.it is working fine but to delete record i need to pass an id that i haave described in the code

------index.php starts

            <!DOCTYPE html><html><head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>A jQuery Confirm Dialog Replacement with CSS3 | Tutorialzine Demo</title>

        <link href='http://fonts.googleapis.com/css?family=Cuprum&amp;subset=latin' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
        <link rel="stylesheet" type="text/css" href="jquery.confirm/jquery.confirm.css" />

        </head>
        <body>

        <div id="page">

            <?php 

          $sql = "SELECT * FROM tablename";
          $result = db_query($sql);

            while(db_fetch($result))
            {
                ?>
                //here we need to pass the fetched record id to script.js file,but i dont know how
                <div class="item">
                <div class="delete"></div>  //here i have applied css i.e it displays wrong icon, when we click on that icon ,it is showing confirmation box. Everything is perfect in this.. but i wnat to pass and id.. im new to jquery
                </div>
                <?php
            }

            ?>
        </div>



        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script src="jquery.confirm/jquery.confirm.js"></script>
        <script src="js/script.js"></script>

        </body>
        </html>
        <!DOCTYPE html>

        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>A jQuery Confirm Dialog Replacement with CSS3 | Tutorialzine Demo</title>

        <link href='http://fonts.googleapis.com/css?family=Cuprum&amp;subset=latin' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
        <link rel="stylesheet" type="text/css" href="jquery.confirm/jquery.confirm.css" />

        </head>
        <body>

        <div id="page">

            <?php 

          $sql = "SELECT * FROM tablename";
          $result = db_query($sql);

            while(db_fetch($result))
            {
                ?>

                <div class="item">
                <div class="delete"></div>
                </div>
                <?php
            }

            ?>
        </div>



        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script src="jquery.confirm/jquery.confirm.js"></script>
        <script src="js/script.js"></script>

        </body>
        </html>

----index.php ends
----jquery.confirm.js file starts

(function($){

    $.confirm = function(params){

        if($('#confirmOverlay').length){
            // A confirm is already shown on the page:
            return false;
        }

        var buttonHTML = '';
        $.each(params.buttons,function(name,obj){

            // Generating the markup for the buttons:

            buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'<span></span></a>';

            if(!obj.action){
                obj.action = function(){};
            }
        });

        var markup = [
            '<div id="confirmOverlay">',
            '<div id="confirmBox">',
            '<h1>',params.title,'</h1>',
            '<p>',params.message,'</p>',
            '<div id="confirmButtons">',
            buttonHTML,
            '</div></div></div>'
        ].join('');

        $(markup).hide().appendTo('body').fadeIn();

        var buttons = $('#confirmBox .button'),
            i = 0;

        $.each(params.buttons,function(name,obj){
            buttons.eq(i++).click(function(){

                // Calling the action attribute when a
                // click occurs, and hiding the confirm.

                obj.action();
                $.confirm.hide();
                return false;
            });
        });
    }

    $.confirm.hide = function(){
        $('#confirmOverlay').fadeOut(function(){
            $(this).remove();
        });
    }

})(jQuery);

----jquery.confirm.js file ends
-----script.js file starts

$(document).ready(function(){

    $('.item .delete').click(function(){

        var elem = $(this).closest('.item');

        $.confirm({
            'title'     : 'Delete Confirmation',
            'message'   : 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
            'buttons'   : {
                'Yes'   : {
                    'class' : 'blue',
                    'action': function(){
                        elem.slideUp();
                                          //sql delete query will be written here... in where condition i need to pass the fetched record id from index.php file  in where condtion    
                    }
                },
                'No'    : {
                    'class' : 'gray',
                    'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
                }
            }
        });

    });

});

-----script.js ends

Upvotes: 0

Views: 3667

Answers (3)

Dharmendra
Dharmendra

Reputation: 216

Here you can use hidden field to save value for id and then use jquery to retrieve it from hidden field..

while(db_fetch($result))
        {
            ?>
            //here we need to pass the fetched record id to script.js file,but i dont know how
            <input type="hidden" id="hid_id" value="<?php echo 'fetched id';?>" />
            <div class="item">
            <div class="delete"></div>  //here i have applied css i.e it displays wrong icon, when we click on that icon ,it is showing confirmation box. Everything is perfect in this.. but i wnat to pass and id.. im new to jquery
            </div>
            <?php
        }
       ?>

And then in jquery when using confirm box you can get value by id hid_id .

Upvotes: 0

Romi Halasz
Romi Halasz

Reputation: 2009

Are you sure you want to send a query from Javascript? The usual way to do this is to send a request (via jQuery) with that specific id, to a script which runs the query (server side), and returns a response.

Now, since you add the item divs, using a while, why not add an id property to the divs, which contain the id from the database, something like

<div id="item<?php echo $row['id'];?>" class="item">
    <div class="delete">
</div>

This way, the $('.item .delete').click handler has access to the item's id, by parsing the target's id property, and you don't need to pass it explicitly to jQuery.

Upvotes: 0

robinef
robinef

Reputation: 312

You can use the html data attribute and retrieve it with .data jQuery method

In your HTML:

<div class="delete" data-id="{$id}"></div>

In your Javascript

$('.item .delete').click(function(){

  var elem = $(this).closest('.item');
  var id = elem.data('id'); /* this is your db id */
});

Upvotes: 3

Related Questions