Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send AJAX on click, but still go to URL of href

Tags:

jquery

ajax

php

I am attempting the following:

When a user clicks a link, I want to update a specific column of a specific table in the database via ajax. BUT I still want the user to be redirected to the href="<url>" of the link.

I tried jQuery without return false; but then the ajax doesn't work.

I tried with return false; but then the page obviously doesn't redirect to the url as I wanted.

Thanks in advance for your help.

like image 684
user1519235 Avatar asked Jul 11 '12 22:07

user1519235


Video Answer


1 Answers

Do your AJAX call, then set document.location when done.

$('a').click(function(e){
    var href = this.href;  // get href from link
    e.preventDefault();  // don't follow the link
    $.ajax({
        url: '/path/to/site',
        data: {some: data},
        success: function(){
            document.location = href;  // redirect browser to link
        }
    });
});
like image 162
Rocket Hazmat Avatar answered Oct 01 '22 00:10

Rocket Hazmat