I'm new to jQuery and I was looking for a way to pass a variable to a new page by POST method WITHOUT using a form.
Using GET is easy:
$("#my_link").click(function(){
var name = 'John';
var url = "my_new_page.php?name=" +name;
window.open(url);
});
How can I do the same thing with POST?
I tried .load(), .post() and .ajax() but it didn't work.
Is it possible?
SOLUTION:
People marked my question as duplicate but that didn't solve my problem.
My thanks to SLaks, it seems that <form>
is the only way.
I made this function that works just fine:
function post2blank(url,myarray)
{ var myform = '<form id="temporary_form" action="' +url+ '" target="_blank" method="POST">';
$.each(myarray, function( key, value ){myform += '<input name="' +key+ '" value="' +value+ '"/>';});
myform += '</form>';
$(myform).appendTo('body').submit();
$('#temporary_form').remove();
}
It creates a <form>
with id="temporary_form" and append it to <body>
. After submit each variable on "myarray" to "result.php", it removes the form.
Here's an example how to call:
<a onclick="var myArray = { name : 'John' , age : '45'}; post2blank('result.php' , myArray);">POST</a>
I tested on Chrome, IE10 and Firefox for android. Hope it helps someone else.