Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect with JQuery, load another page but sent some POST parameters in request?

How do I redirect using either JQuery, DOJO or plain JavaScript, load another page but sent some POST parameters in request?

like image 732
Danka Avatar asked Jun 22 '11 12:06

Danka


2 Answers

This should work, but i haven't tested it:

function postData(url, data)
{
  var form = $('<form></form>');
  $(form).hide().attr('method','post').attr('action',url);
  for (i in data)
  {
    var input = $('<input type="hidden" />').attr('name',i).val(data[i]);
    $(form).append(input);
  }
  $(form).appendTo('body').submit();
}

Basically you can create a form on the fly and submit it. Unfortunately you can not POST stuff directly from script.

like image 88
Ivan Avatar answered Sep 23 '22 23:09

Ivan


You can't redirect with postdata using JavaScript, and I'm sure it's the same for jQuery as well. However, what you can do is...have a hidden form with post data, manipulate it as you need in javascript and submit that form.

<form id="myform" method="post" action="mypage.php">
    <input id="myinput" type="hidden" value="mydata" /> 
</form>

<script type="text/javascript">
    // in some function...
    document.getElementById("myform").submit();
</script>
like image 31
Saad Imran. Avatar answered Sep 24 '22 23:09

Saad Imran.