我使用www.w3schools.com上的示例来了解PHP AJAX和Mysql。我遵循这个例子here.
我已将此company.html上传到我的服务器:
<html>
<head>
<script>
function showCompany(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","get.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="Companies" onchange="showCompany(this.value)">
<option value="">Vælg en virksomhed:</option>
<option value="1">Lego</option>
</select>
</form>
<br>
<div id="txtHint"><b>Data kommer her.</b></div>
</body>
</html>
这个get.php也是:
<?php
$q = intval($_GET['q']);
$con = mysql_connect('localhost','user','password','mydb');
if (!$con) {
die('Could not connect: ' . mysql_error($con));
}
mysqli_select_db($con,"mydb");
$sql="SELECT * FROM Tabel WHERE id = '".$q."'";
$result = mysql_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Virksomhed</th>
<th>Logo/th>
<th>Fokus</th>
<th>Kontaktperson</th>
<th>Mobil</th>
<th>Email</th>
<th>Billede</th>
<th>Om Virksomhed</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CompanyName'] . "</td>";
echo "<td>" . $row['CompanyLogo'] . "</td>";
echo "<td>" . $row['CompanyField'] . "</td>";
echo "<td>" . $row['ContactName'] . "</td>";
echo "<td>" . $row['ContactPhone'] . "</td>";
echo "<td>" . $row['ContactEmail'] . "</td>";
echo "<td>" . $row['ContactImage'] . "</td>";
echo "<td>" . $row['CompanyDetail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
当我尝试获取数据时,我在控制台中收到此消息:
无法加载资源:服务器响应状态为500(内部服务器错误)http://myurl.com/get.php?q=1
我知道这对你们中的一些人来说很简单,并且会感谢所有的反馈。我已多次检查过我的代码,但无法找到错误?
我可能会告诉你一个新手。
两个文件都在同一目录中。
UPDATE !!
我已经在文件中将mysqli更改为mysql。现在我收到此错误消息:
GET http://myurl.com/get.php?q=1 500 (Internal Server Error)
company.html:21
showCompany company.html:21
onchange company.html:28