带有.remove的jQuery函数$.post
jQuery function $.post with .remove
尝试构建一个简单的jQuery,该jQuery允许用户单击表行中的按钮,该按钮应运行单独的PHP脚本,以便在一个操作中从数据库行中删除项目并删除表行。(一旦工作,将迁移到准备好的语句。)
HTML
<tr id="wish_list_row2">
<td class="text-center">
<a href="#" data-toggle="tooltip" data-placement="bottom" title="Click to download this Resource" style="margin-right:15px;"><i class="fa fa-download"></i></a>
<a onclick="deleteWishList(3,1)" data-toggle="tooltip" data-placement="bottom" title="Click to remove this Resource from your Wish List"><i class="fa fa-gift"></i></a>
</td>
</tr>
<tr id="wish_list_row3">
<td class="text-center">
<a href="#" data-toggle="tooltip" data-placement="bottom" title="Click to download this Resource" style="margin-right:15px;"><i class="fa fa-download"></i></a>
<a onclick="deleteWishList(4,1)" data-toggle="tooltip" data-placement="bottom" title="Click to remove this Resource from your Wish List"><i class="fa fa-gift"></i></a>
</td>
</tr>
JS-
function deleteLike(rid,uid) {
$.post("remove_this_item.php?rid="+rid+"&uid="+uid);
$('#row'+rid).remove();
return false;
}
PHP
include_once ('[INCLUDE MYSQL CONNECTION VARIABLES - removed for this demo]');
$rid = $_GET['rid'];
$uid = $_GET['uid'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// delete this 'like' from the db for this user
$sql = "DELETE FROM likes WHERE user_id_REF='$uid' AND resource_id_REF='$rid' LIMIT 1";
$conn->query($sql);
mysqli_close($conn);
当我单击"删除"链接时,tr会按预期删除。但是,数据库行保持不变。如果我在url中手动设置PHP页面上的rid和uid,那么db行将被删除。我尝试过修改一些东西(比如rid和uid顺序),但还没有成功。
我对JS还不是很熟悉,所以也许我在编写函数时搞砸了?
您正在执行$.post
,但在php脚本中,您正在尝试检索$_GET[]
。将其更改为$_POST[]
,它应该可以正常工作。
$.post()
是使用HTML method="POST"
的快捷方式,因此您需要将参数作为表单数据传递,而不是在查询字符串中传递:
$.post( "remove_this_item.php", { rid: rid, uid: uid } );
如果要在查询字符串中传递参数,请使用$.get()
:
$.get("remove_this_item.php?rid="+rid+"&uid="+uid);
相关文章:
- 从类方法中的 ajax post 函数回调函数更改 javascript 类属性
- 在计算AJAX POST函数时遇到问题
- AJAX POST函数未上载文件
- 用于表单验证的 jQuery post 函数只允许 alert() 显示返回的数据
- Javascript 故障破坏了用于选择输入的 POST 函数
- Jquery .ajax POST 函数错误返回 asp.net MVC.
- 如何使用 javascript 使用 angular 2 http post 函数提交表单
- 扩展 jQuery post 函数
- 本机表单提交后的 ajax $.post 函数
- 我的$.post函数没有't返回任何数据
- $.post 函数不起作用
- Node.js中的GET/POST函数
- $.post函数,返回为空
- Mocha正在测试一个post函数
- 如何获取 jquery post 函数的返回值
- 为什么可以't更改Jquery$.post函数(JavaScript)内部的值
- 我可以把带有GIF内容的$.post函数的结果放在javascript图像对象中吗
- Post函数与nodejs和mysql
- 关于AJAX XMLHttpRequest post函数的数据大小是否有限制?
- 为什么我的post函数不能访问它上面的变量?