0

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
Tommy Otzen
  • 190
  • 1
  • 16
  • btw don't give us your url, because your script here is vulnerable to sql injections.... – Freelancer Oct 07 '14 at 12:14
  • Just removed the url. Thx! – Tommy Otzen Oct 07 '14 at 12:17
  • stupid question but do you have a mysql configured on the server ? – Freelancer Oct 07 '14 at 12:18
  • I do. I have also made a file to input data to my db, which works fine. – Tommy Otzen Oct 07 '14 at 12:21
  • Have you check **mysql** in place of **mysqli**, may be your version not supports **mysqli**. – Kausha Mehta Oct 07 '14 at 12:24
  • So if I change mysqli to mysql all the places it might work? – Tommy Otzen Oct 07 '14 at 12:27
  • 1
    http://www.w3fools.com/ – l2aelba Oct 07 '14 at 12:33
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 07 '14 at 12:35
  • A 500 Internal Server error shouldn't have anything to do with your database, it is typically a bad request, some mis-formed PHP code or an .htaccess file that is not properly configured. Do a simple php test with a page containing only `` to confirm that PHP is running and that you can access it. – Jay Blanchard Oct 07 '14 at 12:37

0 Answers0