如何在jQuery成功的情况下执行查询?
How can I execute a query on success of jQuery
我使用jQuery从数据库中删除一些数据。我想要一些功能,当jQuery返回成功时,我想执行一个查询。我想在没有页面刷新的情况下更新jQuery成功的另一个表。我可以这样做吗?如果可以,我该怎么做?
我是jQuery的新手,所以如果这不是一个很好的问题,请不要介意。
这是我的脚本:
<script type="text/javascript">
$(document).ready(function () {
function delete_comment(autoid, btn_primary_ref) {
$.ajax({
url: 'rootbase.php?do=task_manager&element=delete_comment',
type: "POST",
dataType: 'html',
data: {
autoid: autoid
},
success: function (data) {
// I want to execute the Update Query Here
alert("Comment Deleted Successfully");
$(btn_primary_ref).parent().parent().hide();
var first_visible_comment = $(btn_primary_ref).parent().parent().parent().children().find('div:visible:first').eq(0).children('label').text();
if (first_visible_comment == "") {} else {
$(btn_primary_ref).parent().parent().parent().parent().parent().parent().prev().children().text(first_visible_comment);
}
load_comment_function_submit_button(autoid, btn_primary_ref);
},
});
}
$(document).on('click', '.delete_user_comment', function (event) {
var autoid = $(this).attr('id');
var btn_primary_ref = $(this);
var r = confirm("Are you sure to delete a comment");
if (r == true) {
delete_comment(autoid, btn_primary_ref);
} else {
return false;
}
});
});
</script>
你不能直接在Javascript中做数据库操作。您需要做的只是在成功时向后端的php文件发出一个新的AJAX请求,以更新给定的表。但是,这意味着对后端有两个AJAX请求,这两个请求都管理数据库数据。似乎有点多余。为什么不在php文件本身的删除操作之后进行更新操作呢?
添加一个服务器端编码页面来执行您的查询。例子:假设您添加了一个名为executequery.php
的页面。使用下面的代码:
当您想要执行查询时,请执行以下操作:
$.post("executequery.php",//the URL of the page
{
param1:value1,
param2:value2....//if you want to pass some parameters to the page if not set it to null or {}
},
function(data){
//this is the callback that get executed after the page finished executing the code in it
//the "data" variable contain what the page returened
}
);
PS:发送给页面的参数被视为php页面中的$_POST
变量
有一个其他的解决方案,但它的不安全我建议不使用它。它可以发送带有参数的查询,这样你就可以用相同的页面执行任何查询:
$.post("executequery.php",//the URL of the page
{
query:"insert into table values("
param1:value1,
param2:value2....//if you want to pass some parameters to the page if not set it to null or {}
},
function(data){});
相关文章:
- 在不使用隔离作用域的情况下执行函数的角度指令
- Angular在不触发摘要的情况下执行XHR请求
- Phonegap onclick在不单击的情况下执行
- 如何在不冻结浏览器的情况下执行大量javascript代码
- button.click仅在前面有警告的情况下执行
- Node 的断言模块是否提供了一种在没有强制的情况下执行深度相等检查的方法?
- 使用 V8 JavaScript 引擎在没有 Web 视图的情况下执行 JS 库
- 如何在没有物理感谢页面的情况下执行谷歌Adwords转化跟踪
- jQuery,防止在用户双击或多次点击的情况下执行点击事件
- 如何在不单击按钮的情况下执行此功能
- 函数仅在键控事件延迟的情况下执行
- 如何在没有处理程序事件的情况下执行Jquery方法
- 如何在不重定向的情况下执行href
- 是否可以在不执行点击事件的情况下执行内联点击事件函数
- 使用highland.js在引用原始流数据的情况下执行串行异步任务
- 如何在不等待ajax响应的情况下执行计数统计
- 有没有一种快速的工具可以在不剥离JavaScript源代码中的注释的情况下执行恒定的替换?
- 在套接字中发送文件.在不改变用户窗口位置的情况下执行IO操作
- 如何在jQuery成功的情况下执行查询?
- 仅在禁用javascript的情况下执行操作