Like button

2

I have the following button

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

that calls the function that is in the action page that I like the related article, but it happens that when you click the button it redirects to the page action.php and returns to page of news like I do just to perform the action that is in the page action.php without redirecting? someone has an idea

    
asked by anonymous 07.06.2014 / 00:30

2 answers

1

What you need to avoid q link redirect the page is to use .preventDefault() . As the name itself says "prevent the expected / default action".

This code has to be run in javascript and needs an event handler that runs the code when the click is detected and before the link is redirected.

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

Note: In the example of href you have -> . I assume this is not the href already rendered.

    
07.06.2014 / 08:52
0

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

So 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 it in your body :

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

Example: FIDDLE

    
07.06.2014 / 01:31