Sending and Removing from Bank with Jquery

1

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.

    
asked by anonymous 06.09.2017 / 22:51

1 answer

1

Well, the business is to use ajax even with jquery being simple, but your code is bad to understand

1st place an id in your form, <form id='f1'> // file form.php

2nd then separates into two files, which has the form and the php that will receive the data // file receive_form.php

Now the part you asked:

<script> $(document).ready(function(){ $('button').on('click',function(){ $.ajax({ type: 'post', url: 'recebe_form.php', success: function(msg){ $('#f1').html(msg); //aqui o msg é o retorno do php, que será o botao que deve ficar visivel } }); });
}); </script>

Finishing in php, will not use function

if($_POST){ //faz o select para ver se existe o favorito ou nao if($existe){ echo "<button id='remove'>Remove<button>"; //voce nao vai precisar colocar o if else que tava la no form, assim o codigo fica mais separado }else{ echo "<button id='favoritar'>Favoritar<button>"; }
}

It's not the most beautiful code in the world, but it works, you've got an idea I think, see you later.

    
07.09.2017 / 05:29