gpt4 book ai didi

php - 如何在jquery中传递数据库id

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:13 24 4
gpt4 key购买 nike

我想从数据库中删除 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结束

最佳答案

您可以使用 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 */
});

关于php - 如何在jquery中传递数据库id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com