JQuery - message forwarding

How can I redirect with post data?

How to go to a new page using $_POST ?

How to do it? How it is done and why it should be done

+75
javascript jquery html post
Sep 26 '13 at 19:12
source share
11 answers

There is a jQuery plugin that pretty much does what you are trying to do: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js .

After you enable jQuery and the jquery.redirect.min.js plugin, you can simply do something like this:

 $().redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'}); 

Instead, use the following code in newer versions of jQuery:

 $.redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'}); 

Hope this helps!

+82
Sep 26 '13 at 19:26
source share

Here's a simple little function that can be applied everywhere as long as you use jQuery.

 var redirect = 'http://www.website.com/page?id=23231'; $.redirectPost(redirect, {x: 'example', y: 'abc'}); // jquery extend function $.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>').appendTo('body').submit(); } }); 

In the comments, I expanded my answer:

 // jquery extend function $.extend( { redirectPost: function(location, args) { var form = $('<form></form>'); form.attr("method", "post"); form.attr("action", location); $.each( args, function( key, value ) { var field = $('<input></input>'); field.attr("type", "hidden"); field.attr("name", key); field.attr("value", value); form.append(field); }); $(form).appendTo('body').submit(); } }); 
+80
Apr 28 '14 at 17:45
source share

Why not just create a form with some hidden inputs and submit it using jQuery? Must work:)

+8
Sep 26 '13 at 19:18
source share

Before preparing the document / window, add "extend" to jQuery:

 $.extend( { redirectPost: function(location, args) { var form = ''; $.each( args, function( key, value ) { form += '<input type="hidden" name="'+value.name+'" value="'+value.value+'">'; form += '<input type="hidden" name="'+key+'" value="'+value.value+'">'; }); $('<form action="'+location+'" method="POST">'+form+'</form>').submit(); } }); 

Using:

 $.redirectPost("someurl.com", $("#SomeForm").serializeArray()); 

Note: this method cannot create messages.

+3
Feb 16 '15 at 0:09
source share

This requires clarification. Does your server handle the POST you want to redirect somewhere else? Or do you want to redirect the regulatr GET request to another page waiting for POST?

In any case, what you can do is something like this:

 var f = $('<form>'); $('<input>').attr('name', '...').attr('value', '...'); //after all fields are added f.submit(); 

It's probably a good idea to make a link that says “Click here if not automatically redirected” to deal with pop-up blockers.

+1
Sep 26 '13 at 21:56 on
source share

I think this is the best way to do this!

 <html> <body onload="document.getElementById('redirectForm').submit()"> <form id='redirectForm' method='POST' action='/done.html'> <input type='hidden' name='status' value='complete'/> <input type='hidden' name='id' value='0u812'/> <input type='submit' value='Please Click Here To Continue'/> </form> </body> </html> 

It will be almost instant, and the user will not see anything!

+1
Jun 09 '15 at 11:33
source share

Like the answer above, but written in different ways.

 $.extend( { redirectPost: function (location, args) { var form = $('<form>', { action: location, method: 'post' }); $.each(args, function (key, value) { $(form).append( $('<input>', { type: 'hidden', name: key, value: value }) ); }); $(form).appendTo('body').submit(); } }); 
+1
Apr 27 '17 at 14:00
source share

why not just use a button instead of submit. clicking the button will allow you to create the correct URL to redirect your browser to.

 $("#button").click(function() { var url = 'site.com/process.php?'; $('form input').each(function() { url += 'key=' + $(this).val() + "&"; }); // handle removal of last &. window.location.replace(url); }); 
0
Sep 26 '13 at 21:13
source share

I included the jquery.redirect.min.js plugin in the head section along with this json solution for sending data

 <script type="text/javascript"> $(function () { $('form').on('submit', function(e) { $.ajax({ type: 'post', url: 'your_POST_URL', data: $('form').serialize(), success: function () { // now redirect $().redirect('your_POST_URL', { 'input1': $("value1").val(), 'input2': $("value2").val(), 'input3': $("value3").val() }); } }); e.preventDefault(); }); }); </script> 

Then right after the form I added

  $(function(){ $( '#your_form_Id' ).submit(); }); 
0
Oct 23 '13 at 7:14
source share

Create and fill out the hidden method form = POST action = "http://example.com/vote" and submit it, rather than using window.location at all.

or

 $('#inset_form').html( '<form action="url" name="form" method="post" style="display:none;"> <input type="text" name="name" value="' + value + '" /></form>'); document.forms['form'].submit(); 
0
Jul 01 '14 at 15:24
source share

This will redirect with published data

 $(function() { $('<form action="url.php" method="post"><input type="hidden" name="name" value="value1"></input></form>').appendTo('body').submit().remove(); }); } 

the .submit () function automatically sends the URL
.remove () function kills the form after submit

0
Jan 24 '19 at 21:26
source share



All Articles