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

Database

774

Answer

Solution:

Well in the end I didn't even need 2 files. It's just one file now and this is the 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"/>
</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.php">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>

<br><br>        
</div>
<?php
$localhost = "localhost";
$username = "id1419279_root";
$password = "*****";
$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 "<form action='teams.php' method='POST'>";
echo "<select name='teamlijst'>";
echo $dropdownlist;
echo "<input name='submit' type='submit' value='Submit'>";
echo "</select>";
echo "</form>";
}
if(isset($_POST["submit"]) && isset($_POST["teamlijst"])){
if (!$conn) {
    die('Could not connect: ' . mysqli_error($conn));
}
$sql="SELECT * FROM teams WHERE teamnaam = '" . $_POST["teamlijst"] . "'";
$result = mysqli_query($conn,$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($conn);
}
?>
<br>
</div>
</body>
</html>
791

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

$localhost= "localhost";
$username = "id1419279_root";
$password = "nivardenjoey";
$dbname = "id1419279_mlb";

// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname); 
549

Answer

Solution:

1.Your$row is not defined while you were usingwhile($row = mysqli_fetch_array($result)), im wondering whether thiswhile was working.

2.Trydump your SQL results to check if your database settings was going right.

People are also looking for solutions to the problem: php - Converting .JSON list to google maps

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.