How can I redirect with post data?
How to move to new page with $_POST
?
How to do this? How is it done and whyfore shall it be done
I included the jquery.redirect.min.js plugin in the head section together with this json solution to submit with 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 immediately after the form I added
$(function(){
$( '#your_form_Id' ).submit();
});
"extended" the above solution with target. For some reason i needed the possibility to redirect the post to _blank or another frame:
$.extend(
{
redirectPost: function(location, args, target = "_self")
{
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", location);
form.attr("target", target);
$.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();
}
});
Why dont just create a form with some hidden inputs and submit it using jQuery? Should work :)
Here's a simple small function that can be applied anywhere as long as you're using 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();
}
});
Per the comments, I have expanded upon 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();
}
});
why not just use a button instead of submit. clicking the button will let you construct a proper url for your browser to redirect 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);
});
Before document/window ready 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();
}
});
Use :
$.redirectPost("someurl.com", $("#SomeForm").serializeArray());
Note : this method cant post files .