-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:
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
What "doesn’t work"? Is there an error? Checked in the browser console if the request is made?
– tvdias
@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.
– Daniel dos Santos
There is no error, but it is not saved in the bank either.
– Daniel dos Santos
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.
– tvdias
Use Jquery.Ajax() to send a request to the server without updating the page.
– Augusto Vasques
@tvdias, I’m using Torch.
– Daniel dos Santos
@Augustovasques, would not give in the same as what he already uses, the
$.post
?– tvdias
@Augusto Vasques, I’m new to web programming.
– Daniel dos Santos
@tvdias, already clicked F12, nothing appears.
– Daniel dos Santos
Guys, I’ve been struggling with this problem for over a month!
– Daniel dos Santos
Unfortunately I can’t help with Torch, but you can try it with another browser...
– tvdias
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
@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?
– Daniel dos Santos
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
@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.
– Daniel dos Santos
Already updated! @tvdias.
– Daniel dos Santos
This error is pq "$" not found. Usually it is jQuery. Therefore, missing it as a reference to your page.
– tvdias
@tvdias, I added the jQuery link before the <script> tag and it worked! is doing what I wanted! thanks @tvdias!
– Daniel dos Santos