Change a php variable for entire document
I want to change the effects of a set variable that is at the beginning of a document for example:
if(isset($var)){
// Show Loading Message
}
// Whole bunch of code to load a report
unset($var); // To remove the loadi开发者_如何学Gong message
I want the deceleration at the end of the code to remove the loading message that is above by removing the variable. Is there any way to do this with PHP?
Thanks!
Note: I understand how the program would usually work, the question is if there is a way around this typical model output being definite, using PHP.
You are not able to remove something that has already been outputted to the browser using PHP - once you output the loading message, from PHP's perspective it is gone and out the door.
You'd have to have some javascript on the page to manipulate the DOM and remove the text node that way.
For example:
print '<div id="loading_message">Loading, please wait</div>';
... bunch of code ...
print '<script type="text/javascript">var e = document.getElementById("loading_message"); e.parentNode.removeChild(e); </script>';
You will have to do it client side; all HTML generated by a PHP script is sent to the browser and can't be retrieved again. To do it client side, give the loading div an ID of, say, #loading
. Then, at the end of the PHP script, stick some JavaScript in that will hide the div. Here's a jQuery example:
Loading div:
<div id="loading">Loading...</div>
jQuery (echo this from the bottom of your PHP file):
<script> $("#loading").hide(); </script>
EDIT
For a vanilla JavaScript solution, please see @Chris's answer
Hope this helps,
James
Well, there are actually ways to process PHP and send to the browser window BEFORE things are done processing.
It's a hokie, hokie, hokie way of doing things but I wanted to at least share.
You can use the flush()
command at any point to output the current moment in processing out to the browser while the page is still rendering.
You can also do your processing and store the output of the processing in the output buffer before it goes out.
ob_start();
//Do a bunch of PHP stuff
$results = ob_get_contents();
ob_end_clean();
echo $results
Like I said...HOKIE for general web page processing. Use jQuery or your own JS, but maybe this will some in handy somewhere else.
精彩评论