Jump to content

php, html, javascript ajax spewing code to browser


sayhey69

Recommended Posts

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 in a browser.

 

$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.

 

 

<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>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 . i did find this http://php.net/manual/en/language.basic-syntax.phpmode.php (bottom of page) which says to add fake html comments in the php to solve this problem but when i do that there is no output at all. i also found this http://forums.phpfreaks.com/index.php?topic=144158 which is exactly the problem but my file is a .php so im completely lost as to where to go from here

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.