Salve a tutti
ho trovato uno script in rete per aggiungere e rimuovere una icona per preferiti in Ajax
il tutto e' stato adattato al mio codice e al database, ma per qualche strano errore (che non riesco a trovare non visualizzo l'icona per aggiungere e rimuovere)
partiamo dal database, la tabeella favs e' collegata alla tabella actor e al campo actor_id
Codice:
CREATE TABLE `favs` (
`favoriti_id` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`favoriti_id`),
CONSTRAINT `favs_ibfk` FOREIGN KEY (`favoriti_id`) REFERENCES `actor` (`actor_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
codice index.php
Codice PHP:
<script src="jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.button').on('click', function(e){
e.preventDefault();
var actor_id = $(this).attr('actor_id'); // Get the parameter user_id from the button
var favoriti_id = $(this).attr('favoriti_id'); // Get the parameter director_id from the button
var method = $(this).attr('method'); // Get the parameter method from the button
if (method == "Like") {
$(this).attr('method', 'Unlike') // Change the div method attribute to Unlike
$('#' + favoriti_id).replaceWith('<img class="favicon" id="' + favoriti_id + '" src="favon.jpg">') // Replace the image with the liked button
} else {
$(this).attr('method', 'Like')
$('#' + favoriti_id).replaceWith('<img class="favicon" id="' + favoriti_id + '" src="favoff.png">')
}
$.ajax({
url: 'favs.php', // Call favs.php to update the database
type: 'GET',
data: {actor_id: actor_id, favoriti_id: favoriti_id, method: method},
cache: false,
success: function(data){
}
});
});
});
</script>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function checkFavorite($actor_id, $favoriti_id, $conn) {
$result = $conn->query("SELECT * FROM favs WHERE actor_id = '". $actor_id."' AND favoriti_id = '". $favoriti_id."'");
$numrows = $result->num_rows;
if ($numrows == 0) {
echo "<div class = 'button' method = 'Like' actor_id = ".$actor_id." favoriti_id = ".$favoriti_id."> <img id=".$favoriti_id." src='favoff.png'> </div>";
}
else {
echo "<div class = 'button' method = 'Unlike' actor_id = ".$actor_id." favoriti_id = ".$favoriti_id."> <img id=".$favoriti_id." src='favon.jpg'> </div>";
}
}
// Query to get the user_id
$result = $conn->query("SELECT * FROM actor WHERE nome = 'Alex'");
$row = $result->fetch_assoc();
$actor_id = $row['actor_id'];
// Query to Get the Director ID
$result = $conn->query("SELECT * FROM actor WHERE nome = 'Alex'");
$row = $result->fetch_assoc();
$actor_id = $row['actor_id'];
echo "<p>Actor: ".$row['nome']."</p> ";
$fav_image = checkFavorite($actor_id, $favoriti_id, $conn);
echo "Favorite? : ".$fav_image."";
?>
secondo codice favs.php per inserire e rimuovere
Codice PHP:
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$method = $_GET['method'];
//$user_id = $_GET['user_id'];
$actor_id = $_GET['actor_id'];
if ($method == "Like") {
mysqli_query($conn,"INSERT INTO favs (favoriti_id) VALUES ('$actor_id')");
}
else {
mysqli_query($conn,"DELETE FROM favs WHERE favoriti_id = '$actor_id'");
}
?>
grazie per la vostra attenzione