"Like" and "Dislike" buttons don’t work without updating the page

Asked

Viewed 64 times

-1

I’m developing a post system, where in each post viewed, I put a "Like" button and a "Dislike". I implemented a code that updates the page when one of the buttons is clicked, worked perfectly, adding 1, in the total_like or total_nao_gosto. My problem is saving the data dynamically, without updating the page.

Below goes what I’ve tried and doesn’t work:

DB inserir a descrição da imagem aqui

Controller

 // Adicionar gosto sem actualização da página
public function adicionarGosto()
{
    $id = Input::get('post_id');
    $post = Post::find($id);
    if (Cache::has('voto '.$id)==false)
    {
        Cache::add('voto '.$id,'contador',0.30);
        $post->totalgosto_pt+=1;
        $post->save();
        return \Response::json(array('status'=>'sim','qtde'=>$post->totalgosto_pt));
    }
    else
    {
        return \Response::json(array('status'=>'nao'));
    }
}

// Adicionar não gosto sem actualização da página
public function adicionarNaoGosto()
{
    $id = Input::get('post_id');
    $post = Post::find($id);
    if (Cache::has('voto '.$id)==false)
    {
        Cache::add('voto '.$id,'contador',0.30);
        $post->totalnaogosto_pt+=1;
        $post->save();
        return \Response::json(array('status'=>'sim','qtde'=>$post->totalnaogosto_pt));
    }
    else
    {
        return \Response::json(array('status'=>'nao'));
    }
}

View

{{--Botões like e unlike sem actualização da página--}}
<div classe"row">
                           <a class="btn btn-primary _btnGostei">Gostei <i class="glyphicon glyphicon-thumbs-up">({!! $post->totalgosto_pt !!})</i></a>
                            <a class="btn btn-danger _btnNaoGostei">Não Gostei <i class="glyphicon glyphicon-thumbs-down">({!! $post->totalnaogosto_pt !!})</i></a>


              
                        </div>



                        {!! csrf_field() !!}
                        <input type="hidden" name="post_id" value="{!! $post->id_pt !!}">
                        <script type="text/javascript">
                            $("._btnGostei").click(function () {
                                $.post("/portal/adicionar-gosto",{post_id:$('input[name="post_id"]').attr("value"),_token:$('input[name="_token"]').attr("value")},function (response) {
                                    if(response.status=="sim")
                                    {
                                        $("._btnGostei").html("Gostei ("+response.qtde+")");
                                    }

                                });

                            });

                            $("._btnNaoGostei").click(function () {
                                $.post("/portal/adicionar-naogosto",{post_id:$('input[name="post_id"]').attr("value"),_token:$('input[name="_token"]').attr("value")},function (response) {
                                    if(response.status=="sim")
                                    {
                                        $("._btnNaoGostei").html("Não Gostei ("+response.qtde+")");
                                    }

                                });

                            });
                        </script>

Routes

//Gosto e não gosto sem actualização da página

Route::post('/portal/add-like', 'Postcontroller@addirgosto Panel'); Route::post('/portal/add-naogosto', 'Panel Postcontroller@additionogosto');

GUI

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What "doesn’t work"? Is there an error? Checked in the browser console if the request is made?

  • @tvdias am beginner in web programming, do not know how to check in the browser if the request is done. I appreciate you give me some tips for the effect.

  • There is no error, but it is not saved in the bank either.

  • What is your browser? It is usually with the F12, there is a console with the orders (appear only those made after the console is opened) and also the javascript errors.

  • Use Jquery.Ajax() to send a request to the server without updating the page.

  • @tvdias, I’m using Torch.

  • @Augustovasques, would not give in the same as what he already uses, the $.post?

  • @Augusto Vasques, I’m new to web programming.

  • @tvdias, already clicked F12, nothing appears.

  • Guys, I’ve been struggling with this problem for over a month!

  • Unfortunately I can’t help with Torch, but you can try it with another browser...

  • It’s not a hard thing. In this other question is asked exactly the same: https://answall.com/questions/433548/actualizr-div-com-coment%C3%a1rios-a-cada-coment%C3%a1rio-inserido/433571#433571

  • @tvdias, had a problem on the F12 key but I already solved, I was able to find the console and noticed that in the _btnGostei javascript, it is marked with yellow color. What does that mean?

  • With the console open, search for a tab where you display is requests that are made, press one of the buttons and make sure the request is successful.

  • @tvdias, I did and could check an error, I will update the post now, so you can see. It will be the last image of the post.

  • Already updated! @tvdias.

  • 2

    This error is pq "$" not found. Usually it is jQuery. Therefore, missing it as a reference to your page.

  • @tvdias, I added the jQuery link before the <script> tag and it worked! is doing what I wanted! thanks @tvdias!

Show 13 more comments

1 answer

2


This error is pq "$" not found. Usually it is jQuery. Therefore, missing it as a reference to your page.

For that reason, download it on the official website and add this code to your page: <script src="jquery.min.js"></script>, by changing the src according to the location of *.js downloaded.

As an alternative to downloading, it can also be added from a CDN, such as cloudflare: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Browser other questions tagged

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