简体   繁体   中英

php, html, javascript ajax spewing code to browser

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?

You are doing everything right, since the PHP command line works fine.

Here is a possible answer:

"the PHP code after the great-than operator (>) is at risk of being corrupted by the HTML editor's text formatting algorithms.

PHP code with greater-than symbols can be safely embedded into HTML by surrounding it with a pair of HTML-style comment delimiters + fake HTML end & start stags, as PHP-style comments.

Example:

<html>
<body>
Safe-<?php
/*><!--*/
    if (4>3) {
        echo "PHP-";
    }
/*--><?*/
?>embedding
</body>
</html>"

So, in short, you make your code less readable, but by adding in fake html comment tags, you should be able to trick your interpreter into doing the right thing.

Found here (near the bottom of the page): http://php.net/manual/en/language.basic-syntax.phpmode.php

Is you use alert() to display the entire response - or just access the page directly in the browser - you'll see that it's actually returning the entire php file; it's just that your browser is interpreting everything up to that point as one long HTML tag.

This is almost certainly caused by your server not being set to serve .php files with the application/x- httpd-php mime type, instructions for doing which will depend on your server.

edit That's assuming that you actually have a web server set up, rather than directly accessing the files on your hard drive. If not then that'll be your problem and you need to go and install Apache or IIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM