SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    concurrent functions (one javascript, one ajax)

    I am trying to run two functions..one a form submit and the other a simple ajax routine and the idea is to have them both work off one button. Each function works on it's own but when I put them together the form submit works and the ajax one flashes on then off. The code is
    Code:
    <html> 
      <head> 
        <title></title> 
    
        <script language = "javascript">
    
          function getData(url)
    { 
      var XMLHttpRequestObject = false; 
    
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new 
         ActiveXObject("Microsoft.XMLHTTP");
      }
    
      if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("GET", url); 
    
        XMLHttpRequestObject.onreadystatechange = function() 
        { 
          if (XMLHttpRequestObject.readyState == 4 && 
            XMLHttpRequestObject.status == 200) { 
               document.getElementById("targetDiv").innerHTML =
                      XMLHttpRequestObject.responseText; 
              delete XMLHttpRequestObject;
              XMLHttpRequestObject = null;
          } 
        } 
    
        XMLHttpRequestObject.send(null); 
      }
    }
    
    	  function submitform()
    {
      document.myform.submit();  
      
      }
        </script>
    	
      </head> 
    
      <body>
    
    <?
    echo $_POST['test'];
    
    
    
    
    ?>
    <div id="targetDiv">
          <p>Data to go here</p> 
        </div> 
    <form action="<?php echo $editFormAction; ?>" method="post" name="myform">
    <input type="text" name="test">
    
    
    
    <input type="button" name="Button" value="Button" onclick=" submitform(); getData('data.txt');   ">
    </form>
       
        
    
      </body> 
    </html>
    and the data.txt file is just a txt.file with what ever in it.

    Can anyone see what I am doing wrong and how to get this to work?
    Thanks

  2. #2
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tie them together...

    You're trying to execute to javascript at the same time in an onclick event. I generally do this by calling them serially. Call the ajax, then on success, call the next function.

    Or just combine the two like this (untested):

    PHP Code:
     
        
    <script language "javascript">

          function 
    getData(url)

      var 
    XMLHttpRequestObject false

      if (
    window.XMLHttpRequest) {
        
    XMLHttpRequestObject = new XMLHttpRequest();
      } else if (
    window.ActiveXObject) {
        
    XMLHttpRequestObject = new 
         
    ActiveXObject("Microsoft.XMLHTTP");
      }

      if(
    XMLHttpRequestObject) {
        
    XMLHttpRequestObject.open("GET"url); 

        
    XMLHttpRequestObject.onreadystatechange = function() 
        { 
          if (
    XMLHttpRequestObject.readyState == && 
            
    XMLHttpRequestObject.status == 200) { 
               
    document.getElementById("targetDiv").innerHTML =
                      
    XMLHttpRequestObject.responseText
              
    delete XMLHttpRequestObject;
              
    XMLHttpRequestObject null;
          } 
        } 

        
    XMLHttpRequestObject.send(null);  
        
    document.myform.submit();  

      }
    }

        
    </script> 
    mcahill
    Reel-Time.com - Saltwater Fly Fishing
    The Vario Blog
    VarioCreative.com 1 2 3 4 5 6 7


Bookmarks

Posting Permissions

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