我希望从下拉框中获取选定的字段,以便在将来的下拉框中使用它,但我不知道如何通过 ajax 将变量回显到 html。
<p>Trainer</p>
<select name = "trainer_has_update_pokemon">
<option>Select Trainer</option>
<?php
$query = "SELECT name FROM Trainer";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
echo"<option>$name</option>";
}
$stmt->close();
}
?>
</select>
口袋妖怪
<select name = "type_of_update_pokemon">
</select>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('select[name="trainer_has_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
$.ajax({
type:"POST", //send a post method
url:'pkmn_dropdown.php', // path to ajax page
data:"trainer_name="+$(this).val(), //set trainer_name to value
success:function(response){ // retrieve response from php
$('select[name="type_of_update_pokemon"]').html(response); // update select
}
});
});
});
</script>
<select name="nickname_of_update_pokemon">
</select>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('select[name="type_of_update_pokemon"]').change(function(){ // when trainer_has_update_pokemon changes
$.ajax({
type:"POST", //send a post method
url:'nickname_dropdown.php', // path to ajax page
data:"pkmn_name="+$(this).val() & "trainer_name=" +$trainer_name,//set trainer_name to value
success:function(response){ // retrieve response from php
$('select[name="nickname_of_update_pokemon"]').html(response); // update select
}
});
});
});
</script>
名称下拉列表的 php:
<?php
//connect to db
$trainer_name = $_POST['trainer_name'];
$query = "SELECT DISTINCT p.name FROM Pokemon p WHERE p.owner_id = (SELECT t.trainer_id FROM Trainer t WHERE t.name = '$trainer_name')";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($pkmn_name);
while ($stmt->fetch()) {
echo"<option>$pkmn_name</option>";
}
$stmt->close();
echo $trainer_name;
}?>
昵称下拉列表的 php:
<?php
//connect to db
$pkmn_name = $_POST['pkmn_name'];
$query = "SELECT p.nickname FROM Pokemon p, Trainer t WHERE p.name = '$pkmn_name AND p.owner_id = t.trainer_id AND t.name = $trainer_name";
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->bind_result($nickname);
while ($stmt->fetch()) {
echo"<option>$nickname</option>";
}
$stmt->close();
}?>
任何想法如何从第一个下拉框中获取选定的培训师姓名,以便我可以在昵称下拉框中使用它