Search code examples
phpajaxdatabaseinstant

Ajax instant insert in database


I'm trying to make an instant insert and instant deletion in my database. (By instant I mean: no reloading. Just the same as a Facebook like button)

So, with some help, I got on this :

<script>
$(function(){
$('.bouton').click(function(){
    var ID = $(this).attr("id");
    var id_membre = "<?php $_SESSION['membre'] ?>";
    var lus = "<?php echo $lus; ?>";

    if(lus = 0)
    {        
    $.ajax({
        type: "POST",
        url: "add.php",
        data: {"id_livre": id, "id_membre" : id_membre}
    }
    else {
    $.akax({
        type: "POST",
        url: "delete.php",
        data: {"id_livre": id, "id_membre" : id_membre}
    }
    });
});
});
</script>

Clicking on the button, if the user has not read the article (lus = 0), it adds a line in the database, if it has already read (lus = 1) and clicked, it removes the line.

What do you think. I'm sure there is several errors but this is a beginning.

Thank you for your help. Cordially.


Solution

  • if(lus == 0)
    
    else {
    $.ajax({
    

    and I will add a success function :

    success:function(callback){alert("ok");},
    

    and an error function :

    error:function(callback){alert(callback)}