JavaScript 不能自己处理数据库,它只能告诉浏览器该做什么,并且只有在服务器将页面提交给浏览器后才会加载。所以我想你必须使用 AJAX。使用 jQuery 非常简单:
...
$.post('process.php', {id:curid, date : when}, function(data) {
alert(data);
//data contains all output from process.php,
//either in json-format if you jsonencode a results-array
//or just a simple string you echo in the php
return false; //prevent from reloading the page
});
...
您的 process.php 可能类似于:
<?php
$curid = $_POST['id'];
$when = $_POST['date'];
//run the query
echo jsonencode($results_array);
//or check if query succeeded and echo e.g. 'ok' or 'failed'
?>
你明白了... ;)
//编辑:
您可能应该在对话框中使用jQuery UI 以避免出现 cmets 中描述的消息。可能是这样的:
<div id="dialog_box" style="display: none;">
Really delete?
</div>
$('#dialog_box').dialog({
title: 'Really delete this stuff?',
width: 500,
height: 200,
modal: true,
resizable: false,
draggable: false,
buttons: [{
text: 'Yep, delete it!',
click: function(){
//do the post
}
},
{
text: 'Nope, my bad!',
click: function() {
$(this).dialog('close');
}
}]
});