php下拉菜单人口 [英] php dropdown menu population

查看:110
本文介绍了php下拉菜单人口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个php脚本,它会根据主要的下拉菜单的选择来填充第二个下拉菜单。我想使用jquery来完成所有非页面刷新的东西。但是我发现存在的每件事情都难以理解和修改,你知道任何写得很好,易于理解的东西,还是存在于那里的教程?

I'm trying to write a php script that will populate a second drop down menu based on the selection of the a primary drop down menu. I would like to use jquery to do all the non-page refresh stuff. but every thing that I find that exists out there is hard to understand and modify, do you know of anything that was well written and easy to understand or maybe a tutorial that exists out there?

推荐答案

这里有一些代码可以让您了解您想要做什么:

Here's some code that should give you an idea of what you want to do:

HTML

<select id="state" name="state">
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
</select>
<select id="city" name="city">
    <option value="">Please select a state...</option>
</select>

PHP

<?php
    $cities = array(
        'IL' => array( 'Chicago', 'Naperville', 'Decatur', 'Saint Charles' ),
        'IN' => array( 'Gary', 'Miller', 'Portage', 'Merrillville' )
    );

    print json_encode( $cities[ $_POST[ 'state' ] ] );
    exit;
?>

jQuery

jQuery(document).ready(function() {

    jQuery('#state').change(function() {
        jQuery.post(
            'some-url.php',
            {
                'state':jQuery('#state').val()
            },
            function(data, textStatus) {
                jQuery.each(data, function(index, value) {
                    jQuery('#city').append('<option value="' + value + '">' + value + '</option>');
                });
            },
            'json'
        );
    });

});

这篇关于php下拉菜单人口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆