Jump to content

php/ajax instant load???


Guardian-Mage

Recommended Posts

I have this ajax script:

<!--
   var http_request = false;
   function makePOSTRequest(url, parameters) {
	  http_request = false;
	  if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		 }
	  } else if (window.ActiveXObject) { // IE
		 try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) {
			try {
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		 }
	  }
	  if (!http_request) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	  }
	  
	  http_request.onreadystatechange = alertContents;
	  http_request.open('POST', url, true);
	  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  http_request.setRequestHeader("Content-length", parameters.length);
	  http_request.setRequestHeader("Connection", "close");
	  http_request.send(parameters);
   }

   function alertContents() {
	  if (http_request.readyState == 4) {
		 if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById('info').innerHTML = result;            
		 } else {
			alert('There was a problem with the request.');
		 }
	  }
   }
   
   function get() 
   {
	    makePOSTRequest('boot/init.php', 'dummy');
   }
-->

 

and a php file which does a loop, and pauses between each one. I want AJAX to fetch my results as I echo them. I won't put my php script here, but here is the general idea

<?php
$a = 0;
while ($a < 10) {
echo "$a out of 10 complete";
sleep(1);
$a++;
}

So each time php uses the echo command, I want my html script to update, instead of waiting for the entire php script to run. I am using XHTML 1.1 and I have tried changing "if (http_request.readyState == 4) {" to " if (http_request.readyState == 3) {" with no success

Link to comment
Share on other sites

This can't be done the way you want. Your AJAX script waits for the results of the entire file before doing anything.

 

What you can do is change your php script so that rather than a loop, it only processes the script one time, then sends back the information to ajax. Include a variable in this script that your AJAX can then send back to PHP to let it know where it left off, so that it can then go through the script again with the next set of data. When PHP is done, it should send back a new variable to your AJAX function, and so on until it has done the script as many times as it used to do the loop.

Link to comment
Share on other sites

Set your ajax script to call itself at the end of the script as long as the php sends back some variable. Make the php script stop sending back that variable after it has done the action the correct number of times. If that variable isn't sent back, the Ajax script will exit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.