My ajax problem

I’ve been working on making an ajax save to favorite function for my project and I been ending up with this code. Please bear in mind im a noob and I’m sure it could look alot better to advice in addition to the problem is very much appreaciated.

The problem appears when I do a second click to remove it, I get issues with the php hander file. I will post the content of both

$(document).on('click', '.button6', function() {
	const tankID = <?php echo $tankID; ?>;
	const userID = <?php echo $userID; ?>;
	const favX = <?php echo $favX+1; ?>;
	
	var fx = document.getElementById('dataTxt');
	var favorite = fx.getAttribute('data-id');
	console.log("Favorite: " + favorite + "!");
	
	if (favorite == 0) {
		$.post('tanks_favorite_ajax.php', { tankID: tankID, userID: userID, favorite: favorite }, function() {
			document.getElementById("icon").src="https://www.fsd.com/icons/heartarrow.png";
			document.getElementById("dataTxt").innerHTML = "Favorited by "+ favX +" driver(s)";
			favorite = !favorite;
			var d = document.getElementById("dataTxt");
			d.setAttribute('data-id' , favorite); 
			console.log("1.Favorite is now: " + favorite + "!");
		});
	} else {
		$.post('tanks_favorite_ajax.php', { tankID: tankID, userID: userID, favorite: favorite }, function() {
			document.getElementById("icon").src="https://www.fsd.com/icons/arrow.png";
			document.getElementById("dataTxt").innerHTML = "Add to Favorites";
			favorite = !favorite;
			var d = document.getElementById("dataTxt");
			d.setAttribute('data-id' , favorite); 
			console.log("2.Favorite is now " + favorite + "!");
		});   
	}
});
<?php
include('connection.php');
include('functions.php');


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tankID'])) {

    $tankID = (int) $_POST['tankID'];
    $userID = (int) $_POST['userID'];
    $favorite = (int) $_POST['favorite'];

    try {
        if ($favorite == 0) {
            $stmt=$pdo->Prepare("INSERT INTO favorites (tankID, userID)
                VALUES (:tankID, :userID)");

                $stmt->execute(array(
                ':tankID' => $tankID,
                ':userID' => $userID                    
                ));
            //Add credits
            addCredit($userID, $tankID, FAVORITE, 'favorite');                
        }
        elseif ($favorite == 1) {
            $stmt=$pdo->Prepare('DELETE FROM favorites WHERE tankID=:tankID AND userID=:userID');

                $query->execute(array(
                ':tankID' => $tankID,
                ':userID' => $userID
                ));  
        }

    } catch (PDOException $e) {
        echo 'error: ' . $e->getMessage();
    }
} else {
    echo 'Invalid request';
}
?>

I assume you start with favorite = 0. So the first time you click it, it works, and sets favorite to true.
The second click then tries to read the favorite, and either sends a boolean or a null to the script, which dies horribly.
I suspect your problem lies here:
favorite = !favorite;
This expression will evaluate to a boolean, not an int.
!0 = true
!<any int that isnt 0 or NaN> = false.

If its meant to be an int, keep it as an int.

If you’re using an up-to-date version of PHP, check the PHP logs to find out where exactly the PHP script died; if you’re using an older one, check the response from the server for the error message.

The error message will identify the cause of your woes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.