php - Show database information with jquery function
574
I would like to show values in my database of a certain team when I click on the submit button. I have a code, but it doesn't work so please help!! Here's my first code:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png"
sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png"
sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet" type="text/css"/>
<script>
function showUser(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 (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getteam.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "nivardenjoey";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT teamnaam FROM teams";
$result = $conn->query($sql);
$dropdownlist = '';
while($row = mysqli_fetch_array($result)) {
$teamnaam = $row['teamnaam'];
$dropdownlist .="<option value='" . $teamnaam . "'>" . $teamnaam . "
</option>";
}
if(isset($dropdownlist)){
echo "<select name='teamlijst' onchange='showUser(this.value)'>";
echo $dropdownlist;
echo "<input type='submit' value='Submit'>";
echo "</select>";
}
?>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</div>
</body>
</html>
And here's my second code of the retrieving part
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$servername = "localhost";
$username = "id1419279_root";
$password = "nivardenjoey";
$dbname = "id1419279_mlb";
$con = new mysqli($localhost, $username, $password, $dbname);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM teams WHERE teamnaam = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>teamnaam</th>
<th>coach</th>
<th>info</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['teamnaam'] . "</td>";
echo "<td>" . $row['coach'] . "</td>";
echo "<td>" . $row['info'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I really hope anyone can help me
Answer
Solution:
Well in the end I didn't even need 2 files. It's just one file now and this is the code:
Answer
Solution:
i'm unable to make comment
you need to change your servername to localhost
also you should use firebug for javascript debug
the rest of your code seems fine and if you can't get PHP errors you should check you php.ini settings
Answer
Solution:
1.Your
$row
is not defined while you were usingwhile($row = mysqli_fetch_array($result))
, im wondering whether thiswhile
was working.2.Try
dump
your SQL results to check if your database settings was going right.