Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Add POST variables to a href link

Tags:

jquery

In a table I have links like this: test.php?id=1

I want to pass prev_id and next_id to test.php as well. (Purpose of these variables is create next/prev buttons; the list can be sorted by the user so no easy way to find out inside test.php.)

However, I do not want prev_id and next_id to be visible in the URL for the user. Can I format the href like this:

<a href="test.php?id=1" prev_id="5" next_id="45">

and use JQuery to "hijack" the link, keeping id as GET in the URL, but sending prev_id and next_id to test.php as POST?

like image 369
elaxsj Avatar asked Aug 11 '11 07:08

elaxsj


2 Answers

Maybe this is your code:

 <a id="link" href="test.php?id=1" prev_id="5" next_id="45">

and the jquery code

$("a#link").click(function(){

    $.post("test.php",
       {
         prev_id: $(this).attr("prev_id"),
         next_id: $(this).attr("next_id")
       },
       function(data) {
         alert("Data Loaded: " + data);
       }
    );

    return false;
});
like image 182
Erik Márföldi Avatar answered Nov 15 '22 08:11

Erik Márföldi


you can use jquery post

call a function to post your data by onClick event,

 <a onClick="postdata()" prev_id="5" next_id="45">

and the jquery code

function postdata(){
    $.post("test.php", { id: "1", next_id: "2" },
       function(data) {
         alert("Data Loaded: " + data);
       });
}
like image 44
Jayantha Lal Sirisena Avatar answered Nov 15 '22 10:11

Jayantha Lal Sirisena