I'm using the example from www.w3schools.com to learn about PHP AJAX and Mysql. I follow this very example here.
I've uploaded this company.html to my server:
<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>
and this get.php also:
<?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);
?>
When I try to fetch the data, I get this message in console:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://myurl.com/get.php?q=1
I know this would be simple enough for some of you and would appreciate all feedback. I've checked my code several times, but can't find the error?
I'm a newbie as you can probably tell.
Both files are en the same directory.
UPDATE!!
I've changed mysqli to mysql in the files. Now I get this error message:
GET http://myurl.com/get.php?q=1 500 (Internal Server Error)
company.html:21
showCompany company.html:21
onchange company.html:28