简体   繁体   中英

How to display information from database onchange

I made a dropdownlist with values from my database. Now I'd like to show information from that same database when I choose another value in the dropdownlist. For example, I choose the team Angels in my dropdownlist and then I would like to show information, that's in the database, about the team Angels. I would really like to know how to do that. Here is my code:

<?php
$servername = "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 * 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='naamteam' onchange='submit()'>";

echo $dropdownlist;

echo "</select>";
}

?>
<?php

if(isset($_POST['naamteam'])) {

$naamteam = $_POST['teamnaam'];

$query = "SELECT * FROM spelers WHERE naamteam = '$teamnaam'";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result)) {
echo "{$row['teamnaam']} <br>";
echo "{$row['coach']} <br>";
echo "{$row['info']} <br>";
}
}

?>

更好的方法是使用Onchange jquery事件并将该数据与AJAX发送到另一个文件,该文件将获取ajax请求,您可以在其中设置查询以基于onchange值获取数据,然后将结果附加到需要显示的位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM