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.
Here's the php:
<?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<count($dist); $i++)
{
$temp = array_fill(0,count($result),0);
for($j=0; $j<count($result); $j++)
{
if ($result[$j] < $dist[$i]) $temp[$j] = 1;
}
$dist[$i] = array_sum($temp)/count($temp);
}
$temp = array_fill(0,count($dist),0);
for($i=0; $i<count($dist); $i++)
{
if ($dist[$i] < 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<count($result); $i++)
{
if ($result[$i] < $best) $temp[$i] = 1;
}
$less = array_sum($temp)/count($temp);
$temp = array_fill(0, count($result), 0);
for($i=0; $i<count($result); $i++)
{
if ($result[$i] > $best) $temp[$i] = 1;
}
$more = array_sum($temp)/count($temp);
$equal = 1 - $less - $more;
echo "<table border='1'>
<tr>
<th>Best</th>
<th>Less</th>
<th>Equal</th>
<th>More</th>
</tr>";
echo "<tr>";
echo "<td> $best </td>";
echo "<td> $less </td>";
echo "<td> $equal </td>";
echo "<td> $more </td>";
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
and the output I get.
$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); ?>
output when i run php5 with arbitrary input value in terminal is:
<table border='1'>
<tr>
<th>Best</th>
<th>Less</th>
<th>Equal</th>
<th>More</th>
</tr><tr><td> 27 </td><td> 0.48417721518987 </td><td> 0.048523206751055 </td><td> 0.46729957805907 </td></tr></table>
which is what i want.
heres the html/javascript if that helps.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
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 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","funCalc.php?t="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="totals" onchange="hreCalc(this.value)">
<option value="">Select a total:</option>
<option value="5.5">5.5</option>
<option value="6">6</option>
<option value="6.5">6.5</option>
<option value="8.5">8.5</option>
</select>
</form>
<br></br>
<div id="txtHint"><b>Total info will be listed here.</b></div>
</body>
</html>
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?