在从社区获得建议后,我开始研究AJAX / php脚本,以便根据MySQL数据库中的变量检索数据。它现在运行得相当好我现在希望能够点击显示的任何变量,并在记住您的选择时将您带到另一页。这可能吗?
这是我的PokemonFight.php代码:
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getpokemon.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px
}
</style>
<title>Pokemon Fight!</title>
</head>
<body>
<h1><center>Pokemon Fight!</center></h1>
<h2>Select your type!</h2>
<form>
<select name="test" onchange="showUser(this.value)">
<option value="">Select a Pokemon Type!:</option>
<option value="1">Normal</option>
<option value="2">Fire</option>
<option value="3">Fighting</option>
<option value="4">Water</option>
<option value="5">Flying</option>
<option value="6">Grass</option>
<option value="7">Poison</option>
<option value="8">Electric</option>
<option value="9">Ground</option>
<option value="10">Psychic</option>
<option value="11">Rock</option>
<option value="12">Ice</option>
<option value="13">Bug</option>
<option value="14">Dragon</option>
<option value="15">Ghost</option>
<option value="16">Dark</option>
<option value="17">Steel</option>
<option value="18">Fairy</option>
</select>
</form>
<br>
<div id="txtHint"><b>Pokemon info will be listed here...</b></div>
</body>
</html>
这是我的getpokemon.php代码:
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','password','pokemon');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"pokemon");
$sql="SELECT * FROM test WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Pokemon Name</th>
<th>PokedexID</th>
<th>Move 1</th>
<th>Move 2</th>
<th>Move 3</th>
<th>Move 4</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PokedexID'] . "</td>";
echo "<td>" . $row['Pokemon_Move_1'] . "</td>";
echo "<td>" . $row['Pokemon_Move_2'] . "</td>";
echo "<td>" . $row['Pokemon_Move_3'] . "</td>";
echo "<td>" . $row['Pokemon_Move_4'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
答案 0 :(得分:1)
下面的代码是我不理解的,但是用以前的新代码替换我之前的代码,在口袋妖怪名称上添加了一个超链接,链接到我用ID创建的另一个页面PokedexID的值。希望这可以帮助任何像我一样困难的人。
代码之前:
echo "<td>" . $row['PokedexID'] . "</td>";
代码之后:
echo "<td><a href=' viewpokemon.php?PokedexID=" . $row["PokedexID"] . "'>" . $row["Pokemon_Name"] . "</a></td>"