0

My POST method is not working in this code but if I chnage it to GET method then it works fine.

This is my code. If you change it to GET then works just fine. Even with POST, it displays likes from the database but it's not adding more like(s) when I click on the like button.

<form action="" method="POST">
    <input type="hidden" name="blogID" value=<?php echo $blogID;?> />
    <input id="likeButton" <?php echo isset($_POST["like"]) ? "disabled" : "";?> type = "submit" value = "Click to Like this Article" name="like"/> <br><br>
    </form>      
    <br>    

    <?php 

    $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

    if($pageWasRefreshed )
    {
        $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes = $conn->query($likesQuery); 
            if ($likes->num_rows > 0) {
                // output data of each row
                while($row2 = $likes->fetch_assoc()) {
                    echo $row2["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
    }

    else{
        if(isset($_POST['like'])) {
            $blogID=$_POST['blogID'];
            $likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
            $conn->query($likesQuery2);
            $likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes3 = $conn->query($likesQuery3); 

            if ($likes3->num_rows > 0) {
                // output data of each row
                while($row = $likes3->fetch_assoc()) {
                    echo $row["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
        }
        else{
            $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes = $conn->query($likesQuery); 
            if ($likes->num_rows > 0) {
                // outputz data of each row
                while($row2 = $likes->fetch_assoc()) {
                    echo $row2["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
        }
    }   
    ;?>
Stak cla
  • 11
  • 6

2 Answers2

0

In else part, you are trying to check isset($_POST['like']) field in the form which not exists. Update

else{
        if(isset($_POST['like'])) {
            $blogID=$_POST['blogID'];

To

else{ 
        if(isset($_POST['blogID'])) {
            $blogID=$_POST['blogID'];
Manoj Singh
  • 253
  • 1
  • 7
-1

Okay so the problem is not with the isset itself, it is that $pageWasRefreshed is always true and thus not reaching to your $_POST condition

Try putting your $_POST condition first, I think it will work fine

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if(isset($_POST['like'])) {
    $blogID=$_POST['blogID'];
    $likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
    $conn->query($likesQuery2);
    $likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes3 = $conn->query($likesQuery3); 

    if ($likes3->num_rows > 0) {
        // output data of each row
        while($row = $likes3->fetch_assoc()) {
            echo $row["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
} else if($pageWasRefreshed ){
    $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes = $conn->query($likesQuery); 
    if ($likes->num_rows > 0) {
        // output data of each row
        while($row2 = $likes->fetch_assoc()) {
            echo $row2["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
}
else {
    $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes = $conn->query($likesQuery); 
    if ($likes->num_rows > 0) {
        // outputz data of each row
        while($row2 = $likes->fetch_assoc()) {
            echo $row2["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
}
mustafaj
  • 305
  • 1
  • 12
  • I really appreciate your time and help. IT WORKED! – Stak cla Sep 28 '19 at 06:15
  • although when I refresh it, it keeps adding likes. The refresh condition in elseif isn't working. – Stak cla Sep 28 '19 at 06:39
  • Once you submit a form, every time you refresh a page the form will be submitted again. So in your case, if you submit the form and the button is disabled, when you refresh your current page the form will be resubmitted. – mustafaj Sep 28 '19 at 06:52
  • how do I stop that? any suggestions? – Stak cla Sep 28 '19 at 07:15
  • What do you exactly want from this form? If you want the user to like a post one and only one time: then you must store that value in the database that this user liked this particular post. You disable the like button when you check in the database if the post liked by this particular user. If this is not what you are looking for, please specify more – mustafaj Sep 28 '19 at 07:26
  • And after you insert the data in the database, you redirect to your page, so that the $_GET or $_POST values are not stored Check the following link: https://en.wikipedia.org/wiki/Post/Redirect/Get – mustafaj Sep 28 '19 at 07:35