Posted By

FatFolderDesigner on 11/24/10


Tagged

ajax forms auto submit


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

tspitzr
wirenaught


AJAX form autosubmit


 / Published in: JavaScript
 

URL: http://fatfolderdesign.com/48/code/javascript-auto-submit-form

I was working on this for a new site ideal I have, though it might be useful for others. What this does it sent a AJAX POST request every time the as() function is called. Setting the form to run as() onkeyup will essentially make the form submit itself whenever updated. I play to use this as part of an auto submit save system.

You could also eave the method and action of a form blank and have it run this with an onclick on the submit button, submitting the form only when they want to but keeping them on the page.

UPDATE: demo of this working available at the URL.

  1. function as(){
  2. var input = document.getElementById('form').elements
  3. var param = 'savetype=auto';
  4. for(var i=0; i!=input.length; i++){
  5. if(input[i].type != 'submit'){
  6. param = param+'&'+input[i].name+'='+input[i].value
  7. }
  8. }
  9. var ass=new XMLHttpRequest();
  10. ass.open('POST','save.php',true);
  11. ass.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  12. ass.setRequestHeader("Content-length", param.length);
  13. ass.setRequestHeader("Connection", "close");
  14. ass.send(param);
  15. ass.onreadystatechange = function() {
  16. if(ass.readyState == 4 && ass.status == 200) {
  17. alert('your response code goes here');
  18. }
  19. }
  20. }

Report this snippet  

You need to login to post a comment.