2

I made like / dislike buttons in a news feed (like Facebook). I want to change the database when someone clicks on either one of those buttons using PHP. I first made the two buttons in a form but then the PHP would be run on a different page and reload the first page causing you to go back to the top of the news feed (which of course shouldn't happen). Then I tried Javascript, but I needed to pass the id of the post from Javascript to PHP. I tried doing this with Cookies:

document.cookie= and $_COOKIE[]

function like(id) {
    document.cookie="lid="+id;
    <?php query("INSERT INTO `likes`(`uid`, `pid`) VALUES (".$_SESSION['id'] . ",".$_COOKIE['lid'].")"); ?>
}
function dislike(id) {
    document.cookie="lid="+id;
    <?php query("DELETE FROM `likes` WHERE uid=".$_SESSION['id'] . " AND pid=".$_COOKIE['lid']); ?>
}

This didn't work so I added some alerts to understand what was going on.

function like(id) {
    <?php $_COOKIE['lid'] = "10"; ?>
    alert("Like:" + id);
    document.cookie="lid="+id;
    <?php query("INSERT INTO `likes`(`uid`, `pid`) VALUES (".$_SESSION['id'] . ",".$_COOKIE['lid'].")"); ?>
    alert("<?php echo $_COOKIE['lid']; ?>");
}
function dislike(id) {
    <?php $_COOKIE['lid'] = "10"; ?>
    alert("Dislike:" + id);
    document.cookie="lid="+id;
    <?php query("DELETE FROM `likes` WHERE uid=".$_SESSION['id'] . " AND pid=".$_COOKIE['lid']); ?>
    alert("<?php echo $_COOKIE['lid']; ?>");
}

The first alert showed the correct id, but the second alert showed "10".

What did I do wrong?

How should I transfer the id from JavaScript to PHP?

Is there an other good way to run PHP code when someone clicks on a button without the newsfeed going back to the top?

Thanks

1 Answer 1

1

Javascript isn'g going to run PHP.

What happens is the PHP code is run before the page is sent to the user.

After the PHP is interpreted.

before

function dislike(id) {
    <?php $_COOKIE['lid'] = "10"; ?>
    alert("Dislike:" + id);
    document.cookie="lid="+id;
    <?php query("DELETE FROM `likes` WHERE uid=".$_SESSION['id'] . " AND pid=".$_COOKIE['lid']); ?>
    alert("<?php echo $_COOKIE['lid']; ?>");
}

after (what gets sent to the user)

function dislike(id) {
    alert("Dislike:" + id);
    document.cookie="lid="+id;
    alert("10"); //whatever that cookie id was
}

So the code <?php query("DELETE FROM `likes` WHERE uid=".$_SESSION['id'] . " AND pid=".$_COOKIE['lid']); ?> has already run by the time the user sees the page and so the like is deleted

You need to user AJAX to properly do this.

function dislike(id){
    var XHR = new XMLHttpRequest();
    XHR.addEventListener("load", function(){
        //was disliked
    })
    XHR.open("get", "dislike.php?id="+id);
    XHR.send();
}

something like unto that for javascript

dislike.php

<?php
parse_str($SERVER["QUERY_STRING"], $arr);
if(isset($arr["id"]){
 query("DELETE FROM `likes` WHERE uid=".$_SESSION['id'] . " AND pid=".$arr["id"]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's not working. Nothing happens. I don't understand: XHR.addEventListener("load", function(){ //was disliked })
That's the callback that is run when the server returns.
Nothing happens ...the code doesn't work. Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.