I'm trying to use output buffering for the first time and it seems that the buffering only works when I run the php script from the command line. The script in question generates a small form and some javascript. The php pulls in a webpage and uses preg_match to pull some stock values (for example). Since the user is to type one of these values into the form and then click a button to compare his answers with the correct ones (pulled from the other webpage by php), I figured the form could show while the php finished loading and processing the external webpage. Here's what I have:
$valuesToCheck=array("grossMargin"=>"Gross Margin","returnOnAssets"=>"Return on Assets","returnOnEquity"=>"Return on Common Equity","totalDebt_Equity"=>"Total Debt/Equity");
ob_start();
echo "<form>Enter the ".$valuesToCheck[$_GET["val"]]." value here: <input type=text name='studentAnswer' size=5> <input type=button value='Check Answer' onClick=\"compareAnswers()\"><br><small>(include a percent sign where applicable)</small><br>
</form>\n";
ob_end_flush();
echo "<script>
function compareAnswers(){\n";
$url="http://www.smartmoney.com/eqsnaps/index.cfm?story=keyStatistics&symbol=COP";
$vryTmp=file($url);
$vryTmp=implode("",$vryTmp);
$pattern="/(".$valuesToCheck[$_GET["val"]].")(.*?)(<\/tr>)/si";
preg_match($pattern,$vryTmp,$value);
echo "valueToCheck='".strip_HTML($value[2])."'\n";
echo "
if(valueToCheck!=document.forms[0].studentAnswer.value){
alert('wrong-o');
}else{
alert('correct');
}
}
</script>";
function strip_HTML($str){
$str=html_entity_decode($str);
$str=preg_replace('/</',' <',$str);
$str=preg_replace('/>/','> ',$str);
$str=strip_tags($str);
$str=preg_replace('/[\n\r\t]/',' ',$str);
$str=preg_replace('/ /',' ',$str);
$str=trim(chop($str));
return $str;
}
I read on a different forum that sometimes IE needs 1k of page size to render it when using output buffering, but the same thing happens in Firefox. I read the php.net and zend.com pages, but the tips there don't really apply here. (Or maybe I'm just dense). It works like a charm from the command line: the form displays first, then the javascript comes out when the php is finished processing the external page.
Thanks for any suggestions,
Biggus