Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    113
    Thanks
    6
    Thanked 1 Time in 1 Post

    How to run this AJAX request when form is submited

    I have this ajax request code and I know how to make it run onClick, but I would like to run it when my form is submited, regardless of wheather user clicked submit or pressed enter.

    Also, I read an article that says GET is the faster method, true?

    Here is my Ajax request:
    Code:
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") {
      http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      http = new XMLHttpRequest();
    } 
    
    function replace(where, wheretwo, what) {
    
    	http.open("GET", "go.php?id=" + what, true);
    	http.onreadystatechange=function() {
        if(http.readyState == 4) {
    	var data = http.responseText.split("[BRK]");
    	document.getElementById(where).innerHTML = data[0];
    	document.getElementById(wheretwo).innerHTML = data[1];
        }
      }
      http.send(null);
    }

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Than call it onsubmit of the form.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by A1ien51 View Post
    Than call it onsubmit of the form.

    Eric
    Won't the document be dismissed before the request gets a chance to terminate?
    I would suggest calling it in the onsubmit handler but have the onsubmit handler return false to prevent submission. Then call the form's submit method as the last statement in the readyState==4 block.

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Yes the poster would have to cancel the form submission. I am sure they could have figured that out.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •