【问题标题】:jQuery function $.post with .removejQuery 函数 $.post 和 .remove
【发布时间】:2015-10-31 01:55:35
【问题描述】:

尝试构建一个简单的 jQuery,允许用户单击表格行中的按钮,该按钮应运行单独的 PHP 脚本以从 db 行中删除项目并在一个操作中删除表格行。 (一旦工作,将迁移到准备好的语句。)

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 会按预期删除。但是,db 行保持不变。如果我在 url 中手动设置 PHP 页面上的 rid 和 uid,则 db 行将被删除。我已尝试更改一些内容(例如 rid 和 uid 顺序),但尚未取得任何成功。

我对 JS 还不是很熟悉,所以也许我在编写函数时搞砸了?

【问题讨论】:

    标签: javascript php jquery mysql


    【解决方案1】:

    您正在执行 $.post,但在您的 php 脚本中,您正试图检索 $_GET[]。将其更改为$_POST[],它应该可以正常工作。

    【讨论】:

    • 天哪。那是我的愚蠢。谢谢。
    • 会的。它要我再等 4 分钟才能接受。 ;)
    【解决方案2】:

    $.post() 是使用 HTML method="POST" 的快捷方式,因此您需要将参数作为表单数据而不是在查询字符串中传递:

    $.post( "remove_this_item.php", { rid: rid, uid: uid } );
    

    如果要传递查询字符串中的参数,请使用$.get()

    $.get("remove_this_item.php?rid="+rid+"&uid="+uid);
    

    【讨论】:

    • 感谢您的简单回答以及对如何引用帖子数据的见解! :)
    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多