안녕하세요 저는 select2를 사용하여 PHP 내 종속 동적 드롭 다운 목록의 스타일을 지정합니다. 문제는 첫 번째 드롭 다운에서 값을 선택하여 mysql DB에서 두 번째 드롭 다운 목록으로 데이터를 가져 오는 것입니다. 그것은 그것의 기본 스타일로 간다 그래서 여기에 문제를 해결하기 위해 몇 가지 솔루션이 필요 내 파일입니다.
선택 2 스크립트select2 with dependent dynamic dropdown PHP/JQuery
<script type="text/javascript">
$(document).ready(function() {
$(".js-example-basic-single").select2();
});
</script>
의 index.php`
<form name="form1" action="test.php" method="post">
<table>
<tr>
<td>
<select name="brand" id="branddd" class="js-example-basic-single" required>
<option disabled selected required >brand</option>
<?php
$res=mysqli_query($link,"select * from brand");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["id"];?>"><?php echo $row["name"];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="model" id="model" class="js-example-basic-single" required>
<option disabled selected required >model</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
스크립트는 DB 값을 가져올 수 :
이<script type="text/javascript">
function change_brand()
{
var xmlhttp=new XMLHttpRequest() ;
xmlhttp.open("GET","ajax.php?brand="+document.getElementById("branddd").value,false) ;
xmlhttp.send(null) ;
document.getElementById("model").innerHTML=xmlhttp.responseText ;
}
$(function(){
$('#branddd').on('change',function(){
this.value && // if value!="" then call ajax
$.get('ajax.php',{brand:this.value},function(response){
$('select[name="model"]').html(response);
});
});
});
</script>
ajax.php가 :
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"test_db");
$brand=$_GET["brand"];
if($brand!="")
{
$res=mysqli_query($link,"select * from model where brand_id=$brand");
echo "<select>";
while($row=mysqli_fetch_array($res))
{
echo "<option>"; echo $row["name"]; echo "</option>";
}
echo "</select>";
}
?>
덕분에, 그래서 u는 "ID", "이름", "클래스"를 추가하는 방법에 나를 인도하시기 바랍니다 수 있습니다 내 에코 속성을 "
업데이트 됨. 그게 도움이 되니? –
고마워, 마침내 첫 번째 제안을 구해줘. – Mouhssine