POST form with javascript (Non-Ajax)


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. // POST Function
  2. function postwith (to,p) {
  3. var myForm = document.createElement("form");
  4. myForm.method="post" ;
  5. myForm.action = to ;
  6. for (var k in p) {
  7. var myInput = document.createElement("input") ;
  8. myInput.setAttribute("name", k) ;
  9. myInput.setAttribute("value", p[k]);
  10. myForm.appendChild(myInput) ;
  11. }
  12. document.body.appendChild(myForm) ;
  13. myForm.submit() ;
  14. document.body.removeChild(myForm) ;
  15. }
  16.  
  17. // Fire function
  18. postwith( 'post.aspx', {
  19. user:'peter' ,
  20. cc:'aus'
  21. });

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.