Jquery for follower insert

1

Good afternoon,

And the following I have an area where users can follow other users need to know if using jquery when clicking next and it is possible to insert in the table the data and change the button to not follow and another color if and as possible and how can i do jquery

Exemplarytableusers

Code

<script>
$(document.body).on('click', '#follow', function(e) {
    e.preventDefault(); //Evita o comportamento padrão
    $.ajax({
        url: "ajax/processa_seguidores.php",
        type: "POST",
        dataTtype: "JSON",
        data: {
            user_logged: <?php echo $_SESSION['user_id']; ?>, //Aqui vai o usuário que clicou
            user_to_follow: <?php echo $row_foodies->id; ?>, //Aqui vai o id do usuário que gostaria de seguir
        }
    }).done(function(data) {
        if (data.error === 0) {
            $( this ).addClass( "unfollow" );
            $( this ).html( "Unfollow" );
            alert(data.message);
            //Uma sugestão você pode usar o Toastr (http://codeseven.github.io/toastr/)
            //para exibir seus retornos de forma mais elegante ao seu usuário
        } else {
            alert('Opa algum erro ocorreu');
        }
        console.log(data);
    });
})
</script>
<div class="my_account user wow fadeInLeft">
<figure>
    <a href="users/<?php echo $row_foodies->slug; ?>"><img style=" border-top-left-radius:10px; border-top-right-radius:10px;" src="<?php echo $row_foodies->user_foto; ?>" alt="" /></a>
</figure>
<div class="container_user" style="border-bottom-left-radius:10px; border-bottom-right-radius:10px;">
    <p><?php echo utf8_encode(limita_caracteres($row_foodies->fb_nome, 13, false)); ?></p>
    <div style="font-family:Arial, Helvetica, sans-serif; margin-top:-15px; font-size:13px; color:#999;"><?php echo $bar['id']; ?> Opiniões</div> 
    <div style="font-family:Arial, Helvetica, sans-serif; margin:0px 0px 10px 0px; font-size:13px; color:#999;">0 Seguidores</div> 
    <?php
    if($_SESSION['FBID'] || $_SESSION['user_id']){
    ?>
        <div id="#follow" class="seguir_user" style="margin:0px 0px 15px 0px; cursor: pointer;">Seguir</div>
    <?php
    }
    ?>
</div>
    
asked by anonymous 20.02.2015 / 18:28

2 answers

0

I made a simple example for you and you need to complement it clearly for your need, but the basics I believe to be there:

Script

  <script>
        $(document.body).on('click', '#follow', function(e) {
            e.preventDefault(); //Evita o comportamento padrão
            $.ajax({
                url: "addfollower.html",
                type: "POST",
                dataTtype: "JSON",
                data: {
                    user_logged: Y, //Aqui vai o usuário que clicou
                    user_to_follow: X, //Aqui vai o id do usuário que gostaria de seguir
                }
            }).done(function(data) {
                if (data.error === 0) {
                    $( this ).addClass( "unfollow" );
                    $( this ).html( "Unfollow" );
                    alert(data.message);
                    //Uma sugestão você pode usar o Toastr (http://codeseven.github.io/toastr/)
                    //para exibir seus retornos de forma mais elegante ao seu usuário
                } else {
                    alert('Opa algum erro ocorreu');
                }
                console.log(data);
            });
        })
    </script>

Page addfollower.php

    <?php 
    $userOn = filter_input(INPUT_POST, 'user_logged', FILTER_SANITIZE_NUMBER_INT);
    $userToFollow = filter_input(INPUT_POST, 'user_to_follow', FILTER_SANITIZE_NUMBER_INT); //Garante que receberá um inteiro

    //Aqui vai seu sql;
    $SQL = "INSERT INTO FOLLOWER (id_user, id_follower) VALUES ($userOn, $usertoFollow)";
    //Aqui você trata o retorno como preferir;
    return json_encode(['error' => 0, "message" => "Você agora está seguinte: XY|"]);

CSS

.unfollow {
    background-color: #FF123F;
}
  

Also remember that none of this has been tested and may contain some errors   

    
20.02.2015 / 18:48
1

You can not insert data into the database by a client language like Javascript (unless you are using NodeJS and from what I saw is not your case).

You could create a page on your website with the language of your choice, in your case PHP, that receives requests and in these requests you send the data that should be inserted. This asynchronous client and server call is called Ajax . >

You can handle these requests in the same way you treat normal form submission requests with the variable $ _POST or $ _GET of PHP.

jQuery has native functionality that allows you to do this. jQuery Ajax .

And about changing colors, it's not difficult. You can use the jQUery's native CSS or ddClass

    
20.02.2015 / 18:41