How to insert a row in the sql table when clicking the button?

Asked

Viewed 217 times

1

I have an immovable site that I am implementing the option of favorites. When opening the property and clicking favorite, it should insert in a new row in the favorite table, this one below (status 1 is favorite), and if "decrypt" it removes the line:

id|idusuario|imovel|status
1 |   789   | 552  |  1

My question is how to do this dynamically, Realaltime. I have the variables $idusuario and $idimovel.

PHP code to insert or remove from bookmarks, just do not know how to call dynamically, without opening another page:

$query = ("INSERT INTO favoritos (idusuario, imovel, status)
VALUES ('".$idusuario."', '".$idimovel."', '1'");
$db->setQuery($query);
$db->execute();
  • Using $.ajax(), can it be? If you can put an example

  • I don’t understand exactly where the difficulty is. Can you send the data to the server? Already have the connection to the database? The problem is building INSERT SQL code or more than that?

  • @Weessmith can yes, I do not have much knowledge in ajax

  • @bfavaretto do not know how to do it right, I know it can be with ajax, but I do not know how to use well the ajax, the ones I did went wrong, only know if type click and open another page to rotate the instruction to insert a new line

  • What exactly is desclicar ?

  • @Leocaracciolo would be a click to reverse an action already done by another click

  • If it were a input type="checkbox or two input type="radio" I think it would be better than a button that could confuse whether it was clicked or decrypted.

  • checkbox tagged > status=1 unchecked > status = 0. With radio one for status 1 and the other for status 0

  • @Leandromarzullo vc already has php that makes these changes?

  • But there is also the possibility to change the button label

  • Are just ideas for friend Wees Smith to consider in the promised response

  • @Weessmith I have php, I think what Leo Cracciolo is a good, a checkbox, I edited the response with php I have created

  • When removing the bookmark, delete the line or change the status?

  • is to delete the line, right? $query=("DELETE FROM favorites WHERE idusuario = ". $idusuario." AND imovel = ". $idimovel) ;

Show 9 more comments

1 answer

3

The $.ajax():

$('#ID_DO_BOTAO').on('click',function(){
    $.ajax({
        type:"POST",
        url:"SEU_ARQUIVO.php",
        dataType:"Json",
        data:{'favorito':'favorito'},
        success:function(a){
            //retorno do php, ou um reload da pagina/elemento
        }
    });
});

SEU_ARQUIVO.php

if(($_POST['favorito'])&&($_POST['favorito']=='favorito')){
    //primeiro vc executa uma verificação se o usuário ja clicou ("SELECT * FROM favoritos WHERE idusuario = '$idusuario');
    if(usuario ja clicou){
        $query=("DELETE FROM favoritos WHERE idusuario = ".$idusuario." AND imovel = ".$idimovel);
    }else{
        $query = ("INSERT INTO favoritos (idusuario, imovel, status)
        VALUES ('".$idusuario."', '".$idimovel."', '1'");
        $db->setQuery($query);
        $db->execute();
    }
}
  • cool, I’ll test it here

  • a doubt, the $idusuario and $imovel are in the file where I will iserir the right ajax? did not understand how I pass them to the favorite.php

  • make that call with that logic I sent right into this favorito.php so, it gets easier for you I believe

  • I did like you sent (I only removed the if to test first) but nothing happened. in the file where I put $.ajax has $idusuario and $idimovel, the favorite.php takes these values alone?

  • These values are set in the favorite.php not?

  • not they are already on the page: immovel.php has the variables $idusuario and $idimovel. There I put the ajax and created another favorite.php file (which is the php you passed) or I did everything wrong?.. rs

  • you need to pass these values to another page then, using the right session

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.