Posted By

rickygri on 08/10/12


Tagged

form javascript post Vanilla


Versions (?)

POST form with javascript (Non-Ajax)


 / Published in: JavaScript
 

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

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.

  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. });

Report this snippet  

You need to login to post a comment.