ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

使用jQuery,PHP和MySQL加载JSON数据,用于单选按钮

2019-05-18 11:04:00  阅读:148  来源: 互联网

标签:jquery json php mysql pdo


我正在尝试填充第三组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}, function(fruit) {

                var html = '';
                $.each(fruit, function(index, 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', {fruitName: fruitName}, function(fruit) {
                var html = '';
                $.each(fruit, function(index, 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', {fruitVariety: fruitVariety}, function(fruit) {
            var html = '';
                $.each(fruit, function(index, 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,json,php,mysql,pdo
来源: https://codeday.me/bug/20190518/1127342.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有