1

I have a function name HideN which triggers when user click on a link like shown below

<a href="<?php echo $dn5['link']; ?>" onclick="HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>')" class="media"> 

this gives me

<a href="/page/example" onclick="HideN('25','username')" class="media">

Now i want to send an ajax request onclick, then redirect the user to /page/example but it always abort the ajax request and redirect the page

function HideN(t, a) {
    var e = {
        id: t,
        user: a
    };
    $.ajax({
        type: "POST",
        url: "https://www.example.com/ajax/hideN?status=hideIt",
        data: e,
        success: function(t) {}
    })
};

How can i first send an ajax request then redirect the page after success response

the link is dynamically generated from database, every link will be different

6 Answers6

1

You need prevent the default click by event.preventDefault() and redirect on ajax success

<a href="" onclick="HideN(event, '<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>', '<?php echo $dn5['link'];?>')" class="media">

        function HideN(ev,t, a, lk) {
            ev.preventDefault();
            var e = {
                id: t,
                user: a
            };
            $.ajax({
                type: "POST",
                url: "https://www.example.com/ajax/hideN?status=hideIt",
                data: e,
                success: function(t) {
                window.location.href = lk;
}
            })
        };
Avijit
  • 1,253
  • 13
  • 21
0

I think i found myself

<a href="javascript:void(0);" onclick="HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>','<?php echo $dn5['link'];?>')" class="media">

which gives me

<a href="javascript:void(0);" onclick="HideN('25','username','/page/link')" class="media">

function HideN(t, a, c) {
    var e = {
        id: t,
        user: a
    };
    $.ajax({
        type: "POST",
        url: "https://www.example.com/ajax/hideN?status=hideIt",
        data: e,
        success: function(t) {
            window.location.href = c;
        }
    })
};
0

Rather than putting the link in the anchor tag like:

<a href="/page/example" onclick="HideN('25','username')" class="media">

you can put href="javascript:void(0);" so that the link won't actually try to change the page:

<a href="javascript:void(0);" onclick="HideN('25','username')" class="media">

Now your ajax call will complete, and you can redirect inside the success function.

success: function(t) {
    window.location.href = "<?php echo $dn5['link']; ?>";
}

If the javascript is in a separate .js file that you can't change through PHP, you can change the link to:

<a href="javascript:void(0);" onclick="HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>','<?php echo $dn5['link']; ?>')" class="media">

and change the javascript to:

function HideN(t, a, link) {
    var e = {
        id: t,
        user: a
    };
    $.ajax({
        type: "POST",
        url: "https://www.example.com/ajax/hideN?status=hideIt",
        data: e,
        success: function(t) {
            window.location.href = link;
        }
    })
};

Now the link will be passed as a parameter to the HideN function, and can be used to redirect inside the success function.

Another option for the link, as suggested in a comment on the question Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"? is to use the HideN function as the href value:

<a href="javascript:HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>','<?php echo $dn5['link']; ?>')" class="media">

Edit: Changed to href="javascript:void(0);" instead of href="#" after comments and further research suggest that it is better.

Community
  • 1
  • 1
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
-1

If you want to redirect the user only if the ajax is successful then place the redirect window.location.href = 'your redirect page' in the success callback.

Otherwise if you need to redirect either is successful or not, use complete callback.

   function HideN(t, a) {
        var e = {
            id: t,
            user: a
        };
        $.ajax({
            type: "POST",
            url: "https://www.example.com/ajax/hideN?status=hideIt",
            data: e,
            success: function(response) { window.location.href = "http://google.com"}
        })
    };

Later edit @codenoire : you should change the href attribute to "javascript: void(0)" or you can use preventDefault

Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
  • the link is dynamically generated from database, every link will be different –  May 25 '16 at 19:27
  • Then you must return that link to client side. – Razvan Dumitru May 25 '16 at 19:31
  • When you will hit an endpoint with this ajax request probably you are able to compute the link that is dynamically generated. So what you need to do is to return that link in response object and that object will be available in your ajax success callback – Razvan Dumitru May 25 '16 at 19:33
  • 1
    This is a good example. BUT you should change the href attribute to "javascript: void(0)" because you don't want the browser to actually go to the value specified in the href attribute. – Xavier J May 25 '16 at 19:34
-1
<a href="<?php echo $dn5['link']; ?>" onclick="HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['from_user'];?>', '<?php echo $dn5['link']; ?>')" class="media"> 

Then have this function:

function HideN(t, am, link) {
    var e = {
        id: t,
        user: a
    };
    $.ajax({
        type: "POST",
        url: "https://www.example.com/ajax/hideN?status=hideIt",
        data: e,
        success: function(t) {
            window.location = link;
        }
    })
    return false;
};
KiloByte
  • 94
  • 3
-2

add

return false;

to your HideN function last line. or

use

preventDefault 

to stop click event default behavior

https://api.jquery.com/event.preventdefault/

Syed Ekram Uddin
  • 2,907
  • 2
  • 29
  • 33