Button like article

Asked

Viewed 504 times

2

I have the following boot

<a href='?area=ref&amp;acao=curtir&amp;noticia=$noticia->id&amp;perfil=$user'>Curtir  </a>";

which calls the function that is on the action page that likes the related article, but it turns out that when clicking the button it redirects to the action.php page and returns to the news page how do I just execute the action that is on the.php page without redirecting? someone has an idea

2 answers

1

What you need to avoid q link redirecting the page is to use the .preventDefault(). As its name says "prevents expected/default action".

This code has to be run in javascript and needs an event receiver (Event Handler) that runs the code when the click is detected and before the link forwards.

$('a').on('click', function(evento){
    evento.preventDefault();
    // resto do seu código aqui ...
})

Note: in the example of href who placed have ->. I take it that’s not the href already rendered.

0

You can create a listening function whenever a link with the class .curtirAjax is clicked.

Then your HTML would look like this:

<a href="?area=ref&amp;acao=curtir&amp;noticia=$noticia->id&amp;perfil=$user" class="curtirBtn">Curtir</a>

And the Javascript function would be as follows:

function curtirAjax(){
    $('.curtirBtn').on('click',function(e){
        e.preventDefault();
        $.get($(this).attr('href'),function(retorno){
            //mensagem de sucesso aqui
        });
    }); 
}

And to call her on your body:

window.onload = function(){ curtirAjax() }

Example: FIDDLE

Browser other questions tagged

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