Populating a second select box depending of the first select box option

无人久伴 提交于 2019-11-29 11:56:50
Put an id in your <select name="X"> code like <select name="X" id ="X">

Put another select like <select name="Y" id="Y">. Which will be blank.

put this jquery in your page.

$("X").on("change",function(){
    var x_value=$("X").val();
    $.ajax({
        url:'ajax.php',
        data:{brand:x_value},
        type: 'post',
        success : function(resp){
            $("#Y").html(resp);               
        },
        error : function(resp){}
    });
});

in your ajax.php add the query.

<?php
$row = mysqli_query("SELECT modelName from modele WHERE brandName =".$_POST['brand']);
while($row2 = mysqli_fetch_array($row))
    echo '<option value="' . $row2['modelId'] . '">' . $row2['modelName'] . '</option>
?>

Hopefully it will work. Please tell me if you need anything.

Use ajax and on change event together,

method: 1- build a page where you post your queries to. 2- that page should react to the query and sends a respond with the desired list of options. example :

if ($_POST["vehicle"] == car) // send response with array of car names as json obj for example
else // send response with array of motorbikes names

your ajax should receive it and populate the filed on-complete.

If you need more details, I'll be happy to provide it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!