jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用jQuery,PHP和MySQL加载JSON数据,用于单选按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试填充第三组radiobuttons作为以下脚本的补充: http://www.electrictoolbox.com/json-data-jquery-php-radio-buttons/

出于某种原因,我似乎无法用相应的数据填充第三组.它只是保持空白:(

调用populateFruittype()函数只返回[],而populateFruitVariety()正确返回json数据.

getdata.PHP(数据库连接/获取数据)

<?PHP

$dsn = "MysqL:host=localhost;dbname=mydb";
$username = "username";
$password = "password";
$pdo = new PDO($dsn,$username,$password);   

$rows = array();

if(isset($_GET['fruitName'])) {
    $stmt = $pdo->prepare("SELECT DISTinCT variety FROM fruit WHERE name = ? ORDER BY variety");
    $stmt->execute(array($_GET['fruitName']));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if(isset($_GET['fruitVariety'] )) {
    $stmt = $pdo->prepare("SELECT DISTinCT fruittype FROM fruit WHERE variety = ? ORDER BY fruittype");
    $stmt->execute(array($_GET['fruitVariety']));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

echo json_encode($rows);

?>

HTML

<!DOCTYPE html>
<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Toevoegen</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

    <script language="javascript" type="text/javascript">

        function populateFruitVariety() {

            var fruitName = $('input[name=fruitName]:checked').val();

            $.getJSON('getdata.PHP',{fruitName: fruitNamE},function(fruit) {

                var html = '';
                $.each(fruit,function(index,array) {
                    html = html + '<label><input type="radio" name="fruitVariety" value="' + arraY['variety'] + '" />' + arraY['variety'] + '</label> ';
                });
                $('#varieties').html(html);

            });

        }

        function populateFruittype() {

            var fruitVariety = $('input[name=fruitVariety]:checked').val();

            $.getJSON('getdata.PHP',{fruitVariety: fruitVariety},array) {
                    html = html + '<label><input type="radio" name="fruitType" value="' + arraY['fruittype'] + '" />' + arraY['fruittype'] + '</label> ';
                });
                $('#fruittype').html(html);

            });

        }

        $(function() {
            $('input[name=fruitName]').change(function() {
                populateFruitVariety();
            });
        });

        $(function() {
            $('input[name=fruitVariety]').change(function() {
                populateFruittype();
            });
        });


    </script>

</head>
<body>


<form>

    <div>
        <strong>Fruit:</strong>
        <label><input type="radio" name="fruitName" value="Apple"/>Apple</label>
        <label><input type="radio" name="fruitName" value="Banana"/>Banana</label>
        <label><input type="radio" name="fruitName" value="Orange"/>Orange</label>
        <label><input type="radio" name="fruitName" value="Pear"/>Pear</label>
    </div>
    <div>
        <strong>Variety:</strong>
        <span id="varieties"></span>
    </div>
    <div>
        <strong>Type:</strong>
        <span id="fruittype"></span>
    </div>
</form>
</body>
</html>

数据库查询内容可在此处找到:http://www.electrictoolbox.com/mysql-example-table/

只需添加

`fruittype` varchar(50) NOT NULL

并填写一些自定义值.

解决方法

1)DB uddate

alter table fruit ADD columN `fruittype` varchar(50) NOT NULL;
updatE fruit SET fruittype = 'description' /* WHERE name = 'Apple'*/;

2)代码更新

<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Toevoegen</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

    <script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $('input[name=fruitName]').change(function() {
            var fruitName = $('input[name=fruitName]:checked').val();
            $.getJSON('test.PHP',function(fruit) {
                var html = '';
                $.each(fruit,array) {
                    html = html + '<input type="radio" name="fruitVariety" value="' + arraY['variety'] + '" /><label>' + arraY['variety'] + '</label> ';
                });
                $('#varieties').html(html);
            });
        });
        $('input[name=fruitVariety]').live('click',function() {
        var fruitVariety = $('input[name=fruitVariety]:checked').val();
            $.getJSON('test.PHP',function(fruit) {
            var html = '';
                $.each(fruit,array) {
                html = html + '<input type="radio" name="fruitType" value="' + arraY['fruittype'] + '" /><label>' + arraY['fruittype'] + '</label> ';
                });
            $('#fruittype').html(html);
            });
        });
    });
    </script>

</head>
<body>

<form>
    <div>
        <strong>Fruit:</strong>
        <label><input type="radio" name="fruitName" value="Apple"/>Apple</label>
        <label><input type="radio" name="fruitName" value="Banana"/>Banana</label>
        <label><input type="radio" name="fruitName" value="Orange"/>Orange</label>
        <label><input type="radio" name="fruitName" value="Pear"/>Pear</label>
    </div>
    <div>
        <strong>Variety:</strong>
        <span id="varieties"></span>
    </div>
    <div>
        <strong>Type:</strong>
        <span id="fruittype"></span>
    </div>
</form>
</body>
</html>

大佬总结

以上是大佬教程为你收集整理的使用jQuery,PHP和MySQL加载JSON数据,用于单选按钮全部内容,希望文章能够帮你解决使用jQuery,PHP和MySQL加载JSON数据,用于单选按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。