Note that there are some explanatory texts on larger screens.

plurals
  1. POphp, html, javascript ajax spewing code to browser
    text
    copied!<p>So yesterday I decided to learn php, Javascript and html. I wrote a php script that gives the correct output when I run it with php5 in the terminal but when I try to add it to a webpage using Javascript it prints bits and pieces of the last ~1/4 of code along with the table I was trying to print but without the variable values. It does this even if I remove all instances of echo from the script.</p> <p>Here's the php:</p> <pre><code>&lt;?php $foo = $_GET["t"]; $con = mysql_connect('host', 'user', 'pw'); if (!$con) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('db', $con); if (!$db_selected) { die('Can\'t use db: ' . mysql_error()); } $sql = sprintf("SELECT fun FROM table WHERE op = '%s'", $foo); $data = mysql_query($sql); $result = array(); $i = 0; while($row = mysql_fetch_array($data)){ $result[$i] = $row[0]; $i++; } $dist = $name = range(min($result), max($result), .5); for($i=0; $i&lt;count($dist); $i++) { $temp = array_fill(0,count($result),0); for($j=0; $j&lt;count($result); $j++) { if ($result[$j] &lt; $dist[$i]) $temp[$j] = 1; } $dist[$i] = array_sum($temp)/count($temp); } $temp = array_fill(0,count($dist),0); for($i=0; $i&lt;count($dist); $i++) { if ($dist[$i] &lt; 0.5) $temp[$i] = 1; } $best = $name[array_sum($temp)-1]; $less = 0; $more = 0; $temp = array_fill(0, count($result), 0); for($i=0; $i&lt;count($result); $i++) { if ($result[$i] &lt; $best) $temp[$i] = 1; } $less = array_sum($temp)/count($temp); $temp = array_fill(0, count($result), 0); for($i=0; $i&lt;count($result); $i++) { if ($result[$i] &gt; $best) $temp[$i] = 1; } $more = array_sum($temp)/count($temp); $equal = 1 - $less - $more; echo "&lt;table border='1'&gt; &lt;tr&gt; &lt;th&gt;Best&lt;/th&gt; &lt;th&gt;Less&lt;/th&gt; &lt;th&gt;Equal&lt;/th&gt; &lt;th&gt;More&lt;/th&gt; &lt;/tr&gt;"; echo "&lt;tr&gt;"; echo "&lt;td&gt; $best &lt;/td&gt;"; echo "&lt;td&gt; $less &lt;/td&gt;"; echo "&lt;td&gt; $equal &lt;/td&gt;"; echo "&lt;td&gt; $more &lt;/td&gt;"; echo "&lt;/tr&gt;"; echo "&lt;/table&gt;"; mysql_close($con); ?&gt; </code></pre> <p>and the output I get.</p> <p>$best) $temp[$i] = 1; } $more = array_sum($temp)/count($temp); $equal = 1 - $less - $more; echo " Best Less Equal More "; echo ""; echo " $best "; echo " $less "; echo " $equal "; echo " $more "; echo ""; echo ""; mysql_close($con); ?></p> <p>output when i run php5 with arbitrary input value in terminal is:</p> <pre><code>&lt;table border='1'&gt; &lt;tr&gt; &lt;th&gt;Best&lt;/th&gt; &lt;th&gt;Less&lt;/th&gt; &lt;th&gt;Equal&lt;/th&gt; &lt;th&gt;More&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt;&lt;td&gt; 27 &lt;/td&gt;&lt;td&gt; 0.48417721518987 &lt;/td&gt;&lt;td&gt; 0.048523206751055 &lt;/td&gt;&lt;td&gt; 0.46729957805907 &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre> <p>which is what i want.</p> <p>heres the html/javascript if that helps. </p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function hreCalc(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","funCalc.php?t="+str,true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;select name="totals" onchange="hreCalc(this.value)"&gt; &lt;option value=""&gt;Select a total:&lt;/option&gt; &lt;option value="5.5"&gt;5.5&lt;/option&gt; &lt;option value="6"&gt;6&lt;/option&gt; &lt;option value="6.5"&gt;6.5&lt;/option&gt; &lt;option value="8.5"&gt;8.5&lt;/option&gt; &lt;/select&gt; &lt;/form&gt; &lt;br&gt;&lt;/br&gt; &lt;div id="txtHint"&gt;&lt;b&gt;Total info will be listed here.&lt;/b&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>i just started learning these languages and ive tried everything i can think of. the problem seems to be coming from the > signs but I cant figure out how to fix it and as far as i can tell you should be able to use > in php without it causing problems with html. im using ubuntu 12.04 so may be i dont have everything installed that i need?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload