我正在尝试填充第三组单选按钮,作为以下脚本的补充:http://www.electrictoolbox.com/json-data-jquery-php-radio-buttons/
由于某种原因,我似乎无法用相应的数据填充第三个集合。它只是保持空白:(
调用populateFruittype()
函数只返回[ ]
,而populateFruitVariety()
返回正确的json数据。
getdata.php (DB connection/fetch data)
<?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);
?>