如何在jquery中传递数据库id

时间:2013-02-14 10:50:41

标签: php jquery mysql

我想从数据库中删除在while循环中显示的记录但在删除之前我想显示一个确认框。我写的代码在下面。它工作正常但删除记录我需要传递一个我在代码中描述的id

------ index.php开始

            <!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结束
---- jquery.confirm.js文件启动

(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文件结束
----- script.js文件启动

$(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结束

3 个答案:

答案 0 :(得分:3)

您可以使用html数据属性并使用.data jQuery方法

检索它

在您的HTML中:

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

在您的Javascript中

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

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

答案 1 :(得分:0)

您确定要从Javascript发送查询吗?通常的方法是将具有该特定id的请求(通过jQuery)发送到运行查询(服务器端)的脚本,并返回响应。

现在,由于您使用while添加项目div,为什么不向div添加id属性,其中包含数据库中的id,类似

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

这样,$('。item .delete')。click handler可以通过解析目标的id属性来访问项的id,而不需要将它显式传递给jQuery。

答案 2 :(得分:0)

在这里你可以使用隐藏字段来保存id的值,然后使用jquery从隐藏字段中检索它。

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
        }
       ?>

然后在使用确认框的jquery中,您可以通过id hid_id获取值。