Return to Snippet

Revision: 58927
at August 10, 2012 18:41 by rickygri


Updated Code
// POST Function
function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

// Fire function
postwith( 'post.aspx', {
    user:'peter' ,
    cc:'aus'
});

Revision: 58926
at August 10, 2012 01:21 by rickygri


Initial Code
// POST Function
function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

// Fire function
postwith( 'post.aspx', {
    user:'peter' ,
    cc:'aus'
});;

Initial URL
http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/

Initial Description
This javascript function takes the URL of the target page and an associative array of name/values paires and POSTs the data to the supplied URL by dynamically creating a form and then submitting it.

Initial Title
POST form with javascript (Non-Ajax)

Initial Tags
form, javascript, post

Initial Language
JavaScript