使用Ajax和PHP动态选择选项菜单

时间:2015-10-30 09:26:28

标签: php ajax

这是参考链接,http://talkerscode.com/webtricks/dynamic-select-option-menu-using-ajax-and-php.php 这有助于我获得动态选择选项, 但必须修改它以获得选择的选项以显示数据库中的其他列值,这是我得到的

 function fetch_select(val)
    {
       $.ajax({
         type: 'post',
         url: 'fetch_data.php',
         data: {
           get_option:val
         },
         success: function (response) {
           document.getElementById("new_select").innerHTML=response; 
         }
       });
    }

我想要做的是,添加多个id来从数据库中获取数据,比如..

 function fetch_select(val)
    {
       $.ajax({
         type: 'post',
         url: 'fetch_data1.php',
         data: {
           get_option1:val
         },
         success: function (response) {
           document.getElementById("new_select1").innerHTML=response; 
         }
       });
    } 

 function fetch_select(val)
    {
       $.ajax({
         type: 'post',
         url: 'fetch_data2.php',
         data: {
           get_option2:val
         },
         success: function (response) {
           document.getElementById("new_select2").innerHTML=response; 
         }
       });
    }

请参阅演示链接:DEMO

感谢您的回复.......

2 个答案:

答案 0 :(得分:0)

您需要获得第一个选择值才能执行此操作

var state = $("select#state option:selected").attr('value');

$.post('file.php', {state:state}, function(data) {

// do stuff
});

答案 1 :(得分:0)

我可以通过PHP获取(输入的id)(不提交表单), 所以我可以检查选择查询以匹配id ....

<input type="text" id="new_select" >

document.getElementById("new_select").innerHTML=response;

谢谢!