Skin99 Skin99 - 1 year ago 81
Javascript Question

XMLHTTP: Internal server error - issue with GET?

I'm trying to retrieve some data from a MySQL database that I have stored on a server. I'm using PHP, Javascript and AJAX to get the data.

When I run the HTML file (New.html) in Chrome and look at the code using developer tools, it says;

GET 500 (Internal Server Error)

showUser @ New.html:31onchange @ New.html:52

I think this refers to xmlhttp.onreadystatechange, and there is a red X beside the .send() line.

<meta charset="UTF-8">
//Javascript Code
function showUser(str) {
if (str == " ") {
document.getElementById("txtHINT").innerHTML = " ";
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firfox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.reponseText;
xmlhttp.send(); //LINE 31
//CSS for HTML table
table {
width: 100%;
border-collapse: collapse;
table, td, th {
border: 1px solid black;
padding: 5px;
th {
text-align: left;
<!-- Code for Form -->
<select name ="users" onchange="showUser(this.value)"> //LINE 51
<option value=" ">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
<div id="txtHint"><b>MySQL Data should go here</b></div>

Does anyone know how to solve these issues? Perhaps the .open() needs to be placed earlier in the code? Or maybe a handler of some kind is needed?

PHP File:

<title>Latest Attempt</title>
$q = intval($_get['q']);
// put your connection code here
$servername = 'localhost';
$username = 'user';
$password = '12345678';
$dbname = 'ajax_demo';

// create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";

$sql="SELECT * FROM my_DB WHERE id = '".$q."'";
$result = mysqli_query($conn,$sql);

echo "<table>
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
echo "</table>";

Answer Source

You have a few misspells in your code which if you fix them your code will work perfectly:

In you JavaScript

At this line

document.getElementById("txtHint").innerHTML = this.reponseText;

You have misspelled reponseTextand it should be responseText so This line will be like:

document.getElementById("txtHint").innerHTML = this.responseText;

In your PHP

You have to remove all these extra html tags from the top of the page:

    <title>Latest Attempt</title>

and this tag from the bottom of the page:


Then you in this line:

$q = intval($_get['q']);

You have to type $_GET in capital so it will be like:

$q = intval($_GET['q']);

And finally mysqli does not have mysqli_select() function, so you have to remove this line completely:


And now you are good to go :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download