• Another AJAX Problem
    1 replies, posted
Hi, I'm having a small problem with jQuery/AJAX: Essentially, I have two pages. Index.php and backend.php; index.php serves as the front page, backend.php does backend processing. Two pieces of data are sent to backend.php via POST: + url: This is collected from the user by means of a form on index.php + filename: This is a randomly generated filename; it is passed to backend.php which creates this file. The file is an XML file used by the backend code to record its activities. This XML file is then to be read by index.php to allow the activities of the backend code to be reported to the user. The important thing is that backend.php is designed to be run for a very long period of time, it will not load and give a response unless something goes wrong. It simply carries on its function for a long period, recording its activities to the XML file, which is then read by the index.php in order to show the user what the backend code is doing. I have working code that allows me to send the data via HTTP POST to the backend code, however the problem is that the code then waits for a response from backend.php, instead of moving on to the next section of code, which is designed to read the XML file. The code for index.php is: [PHP]<html> <head> <title>Blah!</title> <?php //Generate XML filename: function genfilename() { $name = substr(base64_encode(rand(1000000000,9999999999)),0,10); return($name); } do { $filename = genfilename().".xml"; } while(file_exists("./logs/".$filename)); ?> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> //Use jQuery to send data via POST to backend.php $(document).ready(function(){ $("#submitbutton").click(function(){ var url=$("#url").val(); var file="<?php echo $filename; ?>"; $.post("backend.php", {url:url , filename:file}); //The code stops here, waiting for a response from backend.php that won't come // Code to read XML file created by backend.php (currently incomplete): // I need this to execute after the POST has been sent var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { //Print YEA! if it works $("#events").html("YEA!"); } } xmlhttp.open("GET","<?php echo "./logs/".$filename; ?>",true); xmlhttp.send(); }); }); </script> </head> <body> <h1>Welcome to blah</h1> <p>Please complete the form below: </p> <table> <tr> <td>URL:</td> <td><input type="text" id="url" /></td> <td><button id="submitbutton" type="button">Submit</button></td> </tr> </table> <br/><br/><br/> <span id="events"></span> </body> </html>[/PHP] So what I need to know how to do is read the XML file that will have been created by backend.php; the XML file will be constantly added to as backend.php goes about its activities. Rob
[url]http://api.jquery.com/jQuery.ajax/[/url], just set async true. You still need to setup a semaphore so you know when the script has finished that appropriate bit so you can read the xml.
Sorry, you need to Log In to post a reply to this thread.