[英]How to call select tag id in another select tag in php
我已經在該表單中創建了一個表單,我希望如果用戶從 select 標簽中選擇一個元素,則選擇標簽 2 應該啟用並顯示與該元素相關的值。
<form action="filename.php" method="post" enctype="multipart/form-data" >
<?php while($row =mysqli_fetch_array($result)) {?>
Artist: <?php echo $row['artist'];?>
<select name="to_artist" id = "to_artist" class="form-control">
<option value="<?php echo $row['artist'];?>">Select If you want to change</option>
<?php
$sql1 = mysqli_query($con, "SELECT DISTINCT artist_name FROM album");
$row1 = mysqli_num_rows($sql);
while ($row1 = mysqli_fetch_array($sql1)){
echo "<option value='". $row1['artist_name'] ."'>" .$row1['artist_name'] ."</option>" ;
}
?>
</select>
Album : <?php echo $row['album_name'];?>
<select name="to_album" id = "to_album" class="form-control">
<option value="<?php echo $row['album_name'];?>">Select If you want to change</option>
<?php
$artistname=$_POST['to_artist'];
$sql2 = mysqli_query($con, "SELECT * FROM album where artist_name='$artistname'");
$row2 = mysqli_num_rows($sql2);
while ($row2 = mysqli_fetch_array($sql2)){
echo "<option value='". $row2['album_name'] ."'>" .$row2['album_name'] ."</option>" ;
}
?>
</select>
<?php }?>
<input type="Submit" value="Submit" name="save" id="save"/>
</form>
在這段代碼中,我希望如果用戶選擇一個藝術家姓名,那么與該藝術家專輯相關的將顯示在另一個選擇標簽中,有人可以幫助我嗎?
我建議為此使用 jquery 並對 php 進行 ajax 調用。 例如
<script>
$("#to_artist").change(function () {
var selected_artist= $("#to_artist option:selected").val();
$.ajax({
url: "phpfile.php",
type: "POST",
data:{artist:selected_artist},
success: function (response) {
//here append response to select box.
}
});
});
</script>
<?php
if(isset($_POST['artist'])){
$artistname=$_POST['artist'];
//here your query to get albums for the selected artist and return them.
$sql2 = mysqli_query($con, "SELECT * FROM album where artist_name='$artistname'");
$row2 = mysqli_num_rows($sql2);
while ($row2 = mysqli_fetch_array($sql2)){
echo "<option value='". $row2['album_name'] ."'>" .$row2['album_name'] ."</option>" ;
}
}
?>
//您可以在要附加選項的選擇框中創建一個 div。
前任。
<select>
<div id="appended-options">
</div>
</select>
use this in jquery : $("#appended-options").append(response);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.