Meysam
Meysam

Reputation: 18137

jQuery: How to post data after clicking on a link without ajax?

I've got the following links in my page:

<a href='/post/view/12' id='93' class='notification_link'>your post</a>
<a href='/post/view/13' id='112' class='notification_link'>your post</a>

Whenever the user clicks on any of the above links, I need to post the id of the link (e.g. 93) along with the post id (e.g 12) to the (/post/view) page. But I don't want to do this via ajax. I want to be redirected to the /post/view page as if the user has clicked on the link. So far I've come up with the following code, but don't know how to continue. Any advice would be appreciated.

$(document).ready(function(){
    $(document).on("click", '.notification_link', setNotification);
});

function setNotification(e){
    targetUrl = $(this).attr('href');
    id = $(this).attr('id');
    $.post(targetUrl, { id: $(this).attr('id'), view_id: "12" }, null);
    return false;
};

Upvotes: 0

Views: 1130

Answers (2)

Mohd. Shaukat Ali
Mohd. Shaukat Ali

Reputation: 752

If you really want to do this without ajax then you can try this...

function setNotification(e){
    targetUrl = $(this).attr('href');
    id = $(this).attr('id');
    $('<form/>').append('<input name="id" value="'+ id +'"/><input name="view_id" value="12"/>').attr('action', targetUrl ).attr('method', 'POST').appendTo('body').submit();

    return false; 
};

Upvotes: 3

Tasos K.
Tasos K.

Reputation: 8079

In a similar scenario I used cookies to save the value I didn't want to appear in the URL. Put the handler in the mousedown event so that the cookie will be created before the new page starts to load.

jQuery('a.notification_link').mousedown(function () {
    var myid = jQuery(this).attr('id');

    //here save in a cookie, you can use your own code to save in a cookie
    create_cookie('notification_link', myid, 0);
});

And you can get this value both in the code behind and also in the client side.

Upvotes: 0

Related Questions