ajicles Posted July 15, 2010 Share Posted July 15, 2010 I have kind of a dumb question, but is there some way to clear what I have just echo after a certain amount of time? I have my script and I tried to delay it by using sleep and it just delay the time it took to load the webpage lol. Thanks ~AJ $fp = fsockopen("$server", $port, $errno, $errstr, 30); fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"); while (!feof($fp)) { $content = fgets($fp); } $pattern = "/<SONGTITLE>(.*?)<\/SONGTITLE>/si"; preg_match($pattern, $content, $matches); echo $matches[1]; Quote Link to comment Share on other sites More sharing options...
runthis Posted July 15, 2010 Share Posted July 15, 2010 <script type="text/javascript"> function hide(){ document.getElementById('hideme').style.display = 'none'; } function sethide(){ setTimeout("hide()",5000); } </script> <div onload=\"sethide()\" id=hideme>YOUR STUFF TO HIDE</div> will hide it within 5 seconds Should work .... Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 15, 2010 Share Posted July 15, 2010 With PHP only method you'd have to do a refresh on the page... passing some flag variable. Otherwise display it with in the first place with javascript maybe as suggested by runthis Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 15, 2010 Author Share Posted July 15, 2010 I don't really want to hide it but, I was googling around and there is a cool guy from youtube who said: The easiest way to "clear screen" is pretty ghetto but it works. Just echo new lines where you want to clear the screen =P. while(true){ for ($i=0;$i<30;$i++){ echo "rn"; } } But it doesn't really work for me. I just want to echo the new data over the old. It's only one line but it should work. $fp = fsockopen("$server", $port, $errno, $errstr, 30); fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"); while (!feof($fp)) { $content = fgets($fp); } $pattern = "/<SONGTITLE>(.*?)<\/SONGTITLE>/si"; preg_match($pattern, $content, $matches); echo $matches[1]; I just need to learn how to make the script pause before starting up again. The only thing with sleep command is it just holds up the webpage from loading during the seconds it is waiting :-\ Quote Link to comment Share on other sites More sharing options...
litebearer Posted July 15, 2010 Share Posted July 15, 2010 Take a look at this site. It should give you a very good idea as to accomplish what you are trying to do. http://www.designgala.com/refreshing-div-content-with-ajax/ Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 15, 2010 Share Posted July 15, 2010 When a PHP script is run it doesn't send the page to the browser until it is done processing. You can't send a page tothe browser then have PHP interact with that page by itself. THe user needs to initiate some action to ask the server for new information such as the user clicking a link or JavaScript running in the client's browser that will initiate the request. So, you need a client-side solution. But, in this case, even JavaScript is not needed since there is a simple HTML solution. The meta refresh tag. <meta http-equiv="refresh" content="10;url=http://www.domain.com/pagetoredirect.php"> Put a line like that in the HEAD section of the HTML page and after 10 seconds it will refresh the page by opening the URL http://www.domain.com/pagetoredirect.php Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 16, 2010 Author Share Posted July 16, 2010 When a PHP script is run it doesn't send the page to the browser until it is done processing. You can't send a page tothe browser then have PHP interact with that page by itself. THe user needs to initiate some action to ask the server for new information such as the user clicking a link or JavaScript running in the client's browser that will initiate the request. So, you need a client-side solution. But, in this case, even JavaScript is not needed since there is a simple HTML solution. The meta refresh tag. <meta http-equiv="refresh" content="10;url=http://www.domain.com/pagetoredirect.php"> Put a line like that in the HEAD section of the HTML page and after 10 seconds it will refresh the page by opening the URL http://www.domain.com/pagetoredirect.php I just don't want to have to use a iframe or refresh the page. I'm keep looking for an answer. Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 16, 2010 Author Share Posted July 16, 2010 I found a script but it doesn't do much except display my part of my code lol. Returns: (.*?)<\/SONGTITLE>/si"; preg_match($pattern, $content, $matches); echo $matches[1]; //sleep(2); //} ?> Index.php <script src="ajax.js"></script> <script type="text/javascript"> refreshdiv_parserdiv(); </script> <div name="parserdiv" id="parserdiv"></div> AJAX.JS // The AJAX function... function AJAX(){ try{ xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari return xmlHttp; } catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer return xmlHttp; } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; } catch (e){ alert("Your browser does not support AJAX."); return false; } } } } // Timestamp for preventing IE caching the GET request (common function) function fetch_unix_timestamp() { return parseInt(new Date().getTime().toString().substring(0, 10)) } //////////////////////////////// // // Refreshing the parser // //////////////////////////////// function refreshdiv_parserdiv(){ // Customise those settings var seconds = 5; var divid = "timediv"; var url = "xml grabber.php"; // Create xmlHttp var xmlHttp_one = AJAX(); // No cache var timestamp = fetch_unix_timestamp(); var nocacheurl = url+"?t="+timestamp; // The code... xmlHttp_one.onreadystatechange=function(){ if(xmlHttp_one.readyState==4){ document.getElementById(divid).innerHTML=xmlHttp_one.responseText; setTimeout('refreshdiv_timediv()',seconds*1000); } } xmlHttp_one.open("GET",nocacheurl,true); xmlHttp_one.send(null); } // Start the refreshing process window.onload = function startrefresh(){ setTimeout('refreshdiv_timediv()',seconds*1000); } xml grabber.php $fp = fsockopen("$server", $port, $errno, $errstr, 30); fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"); while (!feof($fp)) { $content = fgets($fp); } $pattern = "/<SONGTITLE>(.*?)<\/SONGTITLE>/si"; preg_match($pattern, $content, $matches); echo $matches[1]; 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.