Hello! I recently made another post here, but it was poorly explained.
I was wanting to make a form that does an insert in the bank and also removes.
I have created a small system of titles, so users can favor those they liked best. It works as follows: The user logs in, searches for the title he or she likes and clicking the favorite button is done an insert, the page reloads and the remove button appears because I made a mysqli_num_rows, to say if it has a line or not of the same title id and user.
<form method="POST">
<input type="hidden" value="<?php echo $row["id"]; ?>" name="id_titulo">
<?php
$amigos = mysqli_query($con,"SELECT * FROM favoritos WHERE usuario='$login_cookie' AND titulo='$id'");
$amigoss = mysqli_fetch_assoc($amigos);
if (mysqli_num_rows($amigos)>=1 AND $amigoss["favoritado"]=="sim") {
?>
<label for="remover" class="remover">
<i class="fa fa-heart" aria-hidden="true"></i>
</label>
<input type="submit" value="Remover" name="remover" id="remover" hidden>
<?php
}else{
?>
<label for="favoritar" class="favoritar">
<i class="fa fa-heart-o" aria-hidden="true"></i>
</label>
<input type="submit" name="add" value="Favoritar" id="favoritar" hidden>
<?php
}
?>
</form>
<?php
function add(){
$login_cookie = $_COOKIE['login'];
if (!isset($login_cookie)) {
header("Location: index.php");
}
include('conexao.php');
$id_titulo = $_POST["id_titulo"];
$ins = "INSERT INTO favoritos (usuario, titulo, favoritado) VALUES ('".$login_cookie."', '".$id_titulo."','sim')";
if(mysqli_query($con, $ins)) {
?>
<script type="text/javascript">
location.href='titulo.php?id='+id_titulo;
</script>
<?php
} else {
echo "Erro ao favoritar!";
echo mysqli_error($con);
}
}
?>
<?php
function remove(){
$login_cookie = $_COOKIE['login'];
if (!isset($login_cookie)) {
header("Location: login.php");
}
include('conexao.php');
$id = $_GET['id'];
$saberr = mysqli_query($con,"SELECT * FROM titulo WHERE id='$id'");
$saber = mysqli_fetch_assoc($saberr);
$titulo = $saber['id'];
$ins = "DELETE FROM favoritos WHERE usuario='$login_cookie' AND titulo='$titulo'";
$conf = mysqli_query($con, $ins) or die(mysqli_error());
if ($conf) {
header("Location: titulo.php?id=".$id);
}else{
echo "<h3>Erro ao remover...</h3>";
}
}
?>
This is the code with pure PHP, but it reloads the page every time I bookmark it and removes it, and I wanted it at the time of the click that it did not reload the whole page. I even managed to do an insert with AJAX, seeing some videos on youtube, but I could not remove it. I wanted to know how I would remove it now and if the condition I made can be used in Jquery.