I've queried and doesn't work out what I've found. Is there any way to redirect to give url with POST method using Javascript or jquery?
11 Answers
Based on Eugene Naydenov's answer, I ended up using this which is able to fill form data also, hope to be useful for others:
function redirectPost(url, data) {
var form = document.createElement('form');
document.body.appendChild(form);
form.method = 'post';
form.action = url;
for (var name in data) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = data[name];
form.appendChild(input);
}
form.submit();
}
// redirectPost('http://www.example.com', { text: 'text\n\ntext' });
Update (2021): Some years later turned out a version that opens a new tab/window also would be useful for me so hopefully this would be useful also, just that make sure this will happen in a click event as browsers should block that otherwise,
function openPostPage(url, data) {
var form = document.createElement('form');
document.body.appendChild(form);
form.target = '_blank';
form.method = 'post';
form.action = url;
for (var name in data) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = data[name];
form.appendChild(input);
}
form.submit();
document.body.removeChild(form);
}
5 Comments
document.body.appendChild(form); to this (for example just before form.submit()), because the HTML spec doesn't allow forms that are not associated with the document to be submitted. See this answer for more information.document.body.removeChild(form); after the submit.XMLHttpRequest to redirect a page from other domain wouldn't allow. Browser will detect it with no Access-Control-Allow-Origin, this is one way to solve CORS issue using a form submit to redirect a post request.Create a form, fill method and action attributes, submit the form.
var redirect = function(url, method) {
var form = document.createElement('form');
form.method = method;
form.action = url;
form.submit();
};
redirect('http://www.example.com', 'post');
jQuery version (but I'd prefer pure JavaScript in this particular case):
var redirect = function(url, method) {
$('<form>', {
method: method,
action: url
}).submit();
};
redirect('http://www.example.com', 'post');
3 Comments
Here the idea is to send data to server while you redirect user to another webpage without using the GET method and maintaining a cosmetic appearance to your URL. So we are going to use the same procedure of sending a Form with POST method.
HTML/Javascript code for creating the form and submitting it to server:
<html>
<body>
<script>
var form = document.createElement("form");
form.method = 'post';
form.action = 'receive.php';
var input = document.createElement('input');
input.type = "text";
input.name = "data";
input.value = "this is the data I'm sending to server through POST";
form.appendChild(input);
form.submit();
</script>
<body>
<html>
Receiving this data in PHP:
<pre>
<?php
var_dump($_POST);
?>
</pre>
In this way, users will be redirected to receive.php with some data being informed in a POST method.
Comments
Actually there is a trick. You create a hidden form and trigger the submit button, with jQuery for example:
<form method="POST" action="newurl.html" id="myform">
</form>
and do a
$('#myform').submit();
I suggest you to use the no-jQuery version posted by Eugene Naydenov
1 Comment
$('#myform').submit() to document.querySelector('#myform').submit().Redirect with POST vars (on jQuery)
var redirect = 'http://www.website.com/page?id=23231';
$.redirectPost(redirect, {x: 'example', y: 'abc'});
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="'+location+'" method="POST">'+form+'</form>').submit();
}
});
Comments
You can use serialize your form and all the data in post. Below is an example.
$("#submit_btn").click(function(){
$('.error_status').html();
if($("form#frm_message_board").valid())
{
$.ajax({
type: "POST",
url: "<?php echo site_url('message_board/add');?>",
data: $('#frm_message_board').serialize(),
success: function(msg) {
var msg = $.parseJSON(msg);
if(msg.success=='yes')
{
return true;
}
else
{
alert('Server error');
return false;
}
}
});
}
return false;
});
1 Comment
Using jQuery post the data and redirect to your desired location like so:
$.ajax({
type: 'POST',
url: 'receivedata.asp',
data: 'formValue=someValue',
success: function(data){
window.location = data;
}
});
You could remove the data: 'formValue=someValue', if there is no data to pass to the page you want to post to.
Comments
huh... use AJAX Jquery:
> $.ajax({
> type: "POST",
> url: "www.example.com/the_page_I_post_to.php",
> data: $('yourform').serialize(),//sent data
> success: function(msg) {
> alert("posted !");
> }