我查看了之前的所有示例,但仍然没有骰子,我有一个基本的php问题。
示例是here。我希望能够点击一个表格,其选项如下所示:
在此代码中明确声明表名称有效:
if($_GET['action'] == 'getOptions'){
$category = $_GET['category'];
$query = "SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='headfirstjson' AND
**`TABLE_NAME`='AmericanOilProduction'**";
$result = db_connection($query);
//echo $result;
$Options = array();
while ($row = mysql_fetch_array($result)) {
$Options[] = $row;
}
echo json_encode(array("Options" => $Options));
exit;
}
通过AJAX传递变量的这种组合不会:
AJAX:
function getOptions(category){
var category = category.value
$.ajax({
url: "getData.php?action=getOptions",
type: "GET",
dataType:"json",
data: {category:category},
success: function(json){
$.each(json.Options,function(){
var option = "<option>"+this.COLUMN_NAME+"</option>"
$('#options').append(option)
});
}
});
}
PHP:
if($_GET['action'] == 'getOptions'){
**$category = $_GET['category']**;
$query = "SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='headfirstjson' AND
`TABLE_NAME`='**.$category.**'";
$result = db_connection($query);
//echo $result;
$Options = array();
while ($row = mysql_fetch_array($result)) {
$Options[] = $row;
}
echo json_encode(array("Options" => $Options));
exit;
}
如果有人可以帮助我,那就太好了!感谢。
答案 0 :(得分:3)
您正在尝试将$category
与其周围的句点连接起来。双引号将扩展变量,使表名为“.AmericanOilProduction。”。将代码更改为删除连接运算符。这里不需要它们。你也应该清理你的输入......
if($_GET['action'] == 'getOptions'){
$category = $_GET['category'];
$query = "SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='headfirstjson' AND
`TABLE_NAME`='$category'";
$result = db_connection($query);
//echo $result;
$Options = array();
while ($row = mysql_fetch_array($result)) {
$Options[] = $row;
}
echo json_encode(array("Options" => $Options));
exit;
}
答案 1 :(得分:1)
我认为 jQuery因为URL本身和data:
选项中存在的URL参数而感到困惑。
请尝试将action
参数添加到data:
选项中:
data: {
category: category,
action: 'getOptions'
},