Reputation: 91
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 http://example.net/Example/getuser.php?q=2 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.
<html>
<head>
<title>New</title>
<meta charset="UTF-8">
//Javascript Code
<script>
function showUser(str) {
if (str == " ") {
document.getElementById("txtHINT").innerHTML = " ";
return;
} 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.open("GET","getuser.php?q="+str,true);
xmlhttp.send(); //LINE 31
}
}
</script>
//CSS for HTML table
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {
text-align: left;
}
</style>
</head>
<!-- Code for Form -->
<body>
<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>
</select>
</form>
<br>
<div id="txtHint"><b>MySQL Data should go here</b></div>
</body>
</html>
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:
<html>
<head>
<title>Latest Attempt</title>
</head>
<?php
$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";
mysqli_select($conn,"ajax_demo");
$sql="SELECT * FROM my_DB WHERE id = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
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>";
mysqli_close($conn);
?>
</html>
Upvotes: 0
Views: 576
Reputation: 2085
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 reponseText
and 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:
<html>
<head>
<title>Latest Attempt</title>
</head>
and this tag from the bottom of the page:
</html>
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:
mysqli_select($conn,"ajax_demo");
And now you are good to go :)
Upvotes: 1