Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Recieving multiple ajax commands

Options
  • 25-07-2006 9:33am
    #1
    Registered Users Posts: 1,086 ✭✭✭


    Have finally dicovered how to perform AJAX commands.

    I can easily perform it on one form value, but I have multiple form values I would like to check.

    I have a Javascript function which iterates through each value and performs a php function using ajax. Only problem is I cannot get the iteration to wait until the last form value is completed and a response is recieved, before starting the next value. Since the php function for each value takes some time by the time the response is recieved I will already have iterated to the 5th or 6th value.

    This brings up an error.
    [Exception... "Component returned failure code: 0x804b000f [nsIXMLHttpRequest.setRequestHeader]" nsresult: "0x804b000f ()" location: "JS frame :: http://localhost/bookshop2/save2.php :: sendRequestTextPost2 :: line 52" data: no]
    

    I think this is because it is recieving / sending multiple httpRequests.

    Any other solutions?


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    don't use php or ajax but could you
    a: send an array of values to the php function and let everything happen there
    b: is there a async property you can set ?


  • Registered Users Posts: 1,086 ✭✭✭Peter B


    Thanks amen,

    I would sent the completed array to the PHP function together, but I would like each value to be completed in real time so the user can see how far the function has completed.

    Someone must have had this problem before fealing with multiple form values.

    Any help?

    Thanks in advance.


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    http_request = new XMLHttpRequest();
    http_request.open('GET', 'http://www.example.org/some.file', true);
    http_request.send(null);


    the true defines if the request is synch or asynch


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Can you post up your code that deals with the opening, sending and handling of the request state changes?


  • Registered Users Posts: 1,086 ✭✭✭Peter B


    Connection Object
    <script type="text/javascript">
    <!--
    	var http = createRequestObject();
    
    	function createRequestObject() {
    		var xmlhttp;
    		try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    	  catch(e) {
    	    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    	    catch(f) { xmlhttp=null; }
    	  }
    	  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
    	  	xmlhttp=new XMLHttpRequest();
    	  }
    		return  xmlhttp;
    	}
     // -->
    </script>
    
    Sending of request
    <script type="text/javascript">
    <!--
    var next=true;
    
    function sendRequestTextPost2(id_, isbn_, subject_, euro_) 
    {
    	next = false;
    	var rnd = Math.random();
    	try{
        http.open('POST',  'serverscript.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = handleResponseText;
    		http.send('id='+id_+'&isbn='+isbn_+'&euro='+euro_+'&subject='+subject_+'&rnd='+rnd);
    	}
    	catch(e){
    	alert("error caught!\n"+e);
    	}
    	finally{}
    }
     // -->
    </script>
    
    Handling response
    <script type="text/javascript">
    <!--
    function handleResponseText() {
    	try{
        if((http.readyState == 4)&& (http.status == 200)){
        	var response = http.responseText;
    		var id_num = response.substring(0,1);
    		document.getElementById("isbn_val" + id_num).innerHTML = response.substring(2,response.length);
    		next=true;
    		}
      }
    	catch(e){}
    	finally{}
    }
     // -->
    </script>
    
    For loop
    //id_num is size of arrays
    for (i=0;i<id_num;i++)
    {
           sendRequestTextPost2(id_arr[i], isbn_arr[i], sub_arr[i], euro_arr[i]);
    }
    


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    You could try implementing a queue. Have an array for the values and go through each value when the HttpRequest object is available, each time poping out the value you process.

    Don't know if this makes sense to you? or if i'm actually understanding you correctly. Maybe you could give a bit more info as i'm not exactly sure what your first post ment.


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    Have you tried modifying the open() call to set synch or asynch (like I said earlier :confused: )
    Otherwise just look here


Advertisement