当我点击提交按钮时,我想在某个团队的数据库中显示值。我有一个代码,但它不起作用所以请帮助!! 这是我的第一个代码:
<!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>
这是检索部分的第二个代码
<!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);
?>
我真的希望有人能帮助我
答案 0 :(得分:1)
最后我甚至不需要2个文件。它现在只是一个文件,这是代码:
<!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>
答案 1 :(得分:0)
我无法发表评论
您需要将服务器名称更改为localhost
你也应该使用firebug进行javascript调试
你的其余代码似乎没问题 如果你不能得到PHP错误,你应该检查php.ini设置
$localhost= "localhost";
$username = "id1419279_root";
$password = "nivardenjoey";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
答案 2 :(得分:0)
1.您使用$row
时未定义while($row = mysqli_fetch_array($result))
,我想知道这个while
是否有效。
2.尝试dump
您的SQL结果,检查您的数据库设置是否正确。