Guardian-Mage Posted February 13, 2008 Share Posted February 13, 2008 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 Quote Link to comment Share on other sites More sharing options...
Isityou Posted February 13, 2008 Share Posted February 13, 2008 Are you using post data to communicate with the AJAX script? Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted February 13, 2008 Author Share Posted February 13, 2008 What? I use post data in my ajax .js file, nowhere in my php file. I am new to AJAX. Quote Link to comment Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted February 14, 2008 Author Share Posted February 14, 2008 I there anyway that I could change my AJAX script to do it the way I wanted? Quote Link to comment Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.