Ad

How To Add Item/post To Favourites

- 1 answer

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 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.

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>

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 target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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);
    }

    ?>

Thanks in advance for any assistance!

Ad

Answer

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

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

  1. var postid = _this.data('$postid');
  2. data : '$postid='+ postid,

instead of

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

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

Same error in add.php:

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

without single-qoutes:

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

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

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...


Debugging hints:

  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
Ad
source: stackoverflow.com
Ad