【问题标题】:How to add item/post to favourites如何将项目/帖子添加到收藏夹
【发布时间】:2016-03-20 13:46:31
【问题描述】:

我正在尝试创建一个按钮,允许用户使用 php 和 jquery (ajax) 收藏某些帖子。我一直在尝试在此处使用This answer 以使其全部正常工作,但是我无法获取特定于要收藏的帖子的帖子ID,而是始终给出最后一个帖子的ID要加载到页面上的帖子。这是我的代码,但我怀疑我可能只是在调整它时犯了一个错误。

我有 3 张桌子;用户、帖子和收藏夹。在用户中,我有用户名密码和 id,在帖子中我有 id(和内容),在收藏夹中我有 id、userid 和 postid。 jQuery:

<script>
    $(document).ready(function() {
    $('.favourite').on('click', null, function() {
    var _this = $(this);
    var postid = _this.data('$postid');
    $.ajax({
      type     : 'POST',
      url      : '/add.php',
      dataType : 'json',
      data     : '$postid='+ postid,
      complete : function(data) {
           if(_this.siblings('.favourite'))
           {
             _this.html('<img src="add2.png" />');
           }
           else
           {
             _this.html('<img src="add1.png />');
           }
        }
        });
    });
});
</script>

主 PHP (index.php):

    <?php
    $getposts = mysql_query("SELECT * FROM Posts ORDER BY id DESC") or die(mysql_query());
    while ($row = mysql_fetch_assoc($getposts))
    {
    $id = $row['id'];
    $user = $_SESSION['user'];
    $findid = mysql_query("select * from Users where username='$user'");

    if ($rows = mysql_fetch_assoc($findid));
    {
        $userid= $rows['id'];
        $postid= $id;
    }
    echo '<a href="#" class="favourite" data-id="' . $postid . '"><img src="add1.png" /></a>';
    }
    ?>

(还有其他代码,但与本节无关。)

添加.php:

    <?php
    session_start();
    require_once('connect.php');

    $userid = $_SESSION['$id'];
    $postid = $_SESSION['$postid'];

    $query_favorite = "SELECT userid, postid FROM Favourites";
    $favorite = mysql_query($query_favorite) or die(mysql_error());
    $row_favorite = mysql_fetch_assoc($favorite);
    $totalRows_favorite = mysql_num_rows($favorite);

    if(in_array($_POST['id'], $row_favorite))
    {
    $Del="DELETE FROM Favourites WHERE userid='$userid' AND postid='$postid'";
    $result = mysql_query($Del);

    }
    else
    {
    $Add = "INSERT INTO Favourites (userid, postid) VALUES ('$userid', '$postid')";
    $result = mysql_query($Add);
    }

    ?>

提前感谢您的帮助!

【问题讨论】:

    标签: php jquery mysql ajax database


    【解决方案1】:

    在 main.php 中,您使用的是data-id="' . $postid . '"。 哪个将 postId 插入到 HTML 中,对吗?

    但是在您的 Javascript 中,您正尝试使用

    获取此数据元素
    1. var postid = _this.data('$postid');
    2. data : '$postid='+ postid,

    而不是

    1. var post_id = _this.data('id');
    2. data : 'id='+ post_id,

    因为你的数据属性不是data-$postid,而是data-id

    add.php 中的相同错误:

    1. $userid = $_SESSION['$id'];
    2. $postid = $_SESSION['$postid'];

    没有单引号:

    1. $userid = $_SESSION[$id];
    2. $postid = $_SESSION[$postid];

    因为,当你单引号变量,那么它的字符串“$id”, 不在 $_SESSION 中。

    并且您必须从您的 AJAX 请求发送的 $_POST 数据中获取 $id(postid)。缺少的代码是:json_decode传入POST并抓取id,然后使用...


    调试提示:

    1. 测试数据是否插入HTML
    2. var_dump($_POST); 添加到add.php 以查看AJAX 请求发布的数据。 “id”应该是其中的一部分。
    3. json_decode()传入$_POST获取ID
    4. 然后在其他变量上使用它

    【讨论】:

    • 好吧..对不起,我可能有点慢(睡眠不多+ js noob),但你能解释一下为什么这很糟糕吗?谢谢
    • 是的,它没有正确地从数据属性中获取值并将其插入到 Ajax POST 中。然后在接收端(add.php)再次出现同样的错误,您需要接受来自 AJAX 帖子的传入 JSON 数据,对其进行 json_decode,提取 id,然后开始使用它。
    • 好的,所以我意识到我没有为用户 ID 启动会话,所以我添加了 $_SESSION['id'] = $userid;到 main.php 并且我添加了 $postid = json_decode(file_get_contents('php://input'), true);添加.php。现在我得到了用户 ID,但没有 postid。我不认为我正在正确请求 AJAX 数据...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多