如何将项目/帖子添加到收藏夹 [英] How to add item/post to favourites

查看:48
本文介绍了如何将项目/帖子添加到收藏夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个按钮,该按钮将允许用户使用php和jquery(ajax)收藏某些帖子.我一直在尝试在此处使用此答案,但在获取特定于该收藏夹的帖子的帖子ID时遇到了麻烦,相反,它总是提供要加载到页面上的最后一个帖子的帖子ID.这是我的代码,但是我怀疑我在修改代码时可能犯了一个错误.

I'm trying to create a button which will allow users to favourite certain posts using php and jquery (ajax). I've been trying to use This answer on here to get it all working, but I'm having trouble with getting the post id specific to the post that is meant to be favourited and instead it always gives the post id of the last post to be loaded on the page. Here's my code as it is, but I suspect I've probably just made a mistake in adapting it.

我有3张桌子;用户,帖子和收藏夹.在用户"中,我具有用户名密码和ID,在帖子"中具有ID(和内容),在收藏夹"中,我具有ID,用户ID和PostID.jQuery:

I have 3 tables; Users, Posts and Favourites. In Users I have username password and id, Posts I have id (and content) and in Favourites I have id, userid and 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):

Main 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>';
    }
    ?>

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

(There is other code, but it's not related to this section.)

add.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);
    }

    ?>

在此先感谢您的帮助!

推荐答案

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

In main.php you are using data-id="' . $postid . '". Which inserts the postId into the HTML, right?

但是在您的Javascript中,您尝试使用以下方式获取此数据元素

But in your Javascript you are trying to fetch this data element with

  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 .

Because your data attribute isn't data-$postid, but data-id.

add.php 中的相同错误:

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

没有单行内容

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

因为,当您单引号引用变量时,然后是字符串"$ id",不在$ _SESSION中.

Because, when you single-quote the variable, then its the string "$id", which isn't in $_SESSION.

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

and you have to fetch $id(postid) from the $_POST data send with your AJAX request. The code missing is: json_decode incoming POST and grab the id, then use it...

调试提示:

  1. 测试数据是否已插入HTML
  2. var_dump($ _ POST); 添加到 add.php ,以便查看AJAX请求发布的数据."id"应该是其中的一部分.
  3. json_decode()传入的 $ _ POST 以获取ID
  4. 然后将其用于其他变量
  1. test that the data is inserted into HTML
  2. add var_dump($_POST); to add.php in order to see the data the AJAX requests posts. The "id" should be part of it.
  3. json_decode() the incoming $_POST to get the ID
  4. and then use it on other variables

这篇关于如何将项目/帖子添加到收藏夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆