所以昨天我決定學習PHP,JavaScript和HTML。我寫了一個PHP腳本,讓正確的輸出,當我在PHP5運行在終端,但是當我嘗試將其添加到使用Javascript它打印位和代碼的最後〜1/4的塊與表我沿着網頁試圖打印但沒有變量值。即使我從腳本中刪除了所有的回聲實例,它也會這樣做。PHP,HTML,JavaScript的AJAX代碼噴涌到瀏覽器
這裏的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);
?>
和輸出我得到。
$最好)$溫度[$ I] = 1; } $ more = array_sum($ temp)/ count($ temp); $ equal = 1 - $ less - $ more;回聲「最好不少於平等」;回聲「」;回聲「最好」;回聲「$少」;回聲「$等於」;回聲「$ more」;回聲「」;回聲「」; mysql_close($ CON); ?當我在終端運行使用任意輸入值的php5>
輸出是:
<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>
這是我想要的。
繼承人是否有幫助的HTML/JavaScript的。
<!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>
我剛開始學習這些語言,並且我嘗試了所有我能想到的。問題似乎來自>標誌,但我無法弄清楚如何解決它,據我可以告訴你應該能夠使用>在PHP中沒有它導致問題的HTML。即時通訊使用Ubuntu的12.04所以可能是我沒有安裝我需要的一切?
因爲javascript是客戶端,而php是服務器端,所以你不能使用js輸出php,但是你可以用php輸出js – 2012-07-12 00:30:23
如果你直接在瀏覽器中運行「funCalc.php」,你會得到什麼輸出從XMLHTTPRequest調用它? – 2012-07-12 00:31:05
首先:SQL注入。我的網站是它的受害者。我吸取了教訓:保持備份和消毒輸入。 – 2012-07-12 00:31:35