【发布时间】:2016-02-12 18:15:39
【问题描述】:
我有两张桌子 tbl_category,tbl_food。我正在使用 Cordova,所以我使用一个 html 文件、一个远程 .php 文件,并且我想从类别为“ID”的 tbl_food 中选择所有(可以是 1 或 2 或 3...)。
我对 php 文件使用以下代码:
<?php
include "db.php";
if(isset($_POST['check']))
{
$ID=$_POST['ID'];
$data=array();
$q=mysql_query("select * from `tbl_food` WHERE Cat='$ID'");
while ($row=mysql_fetch_assoc($q)){
$data[]=$row;
echo json_encode($data);
}
}
?>
这是我在 HTML 文件中使用的 ajax 代码,一旦我从下拉列表中选择类别,就会调用该函数:
function getFoodList() {
var ID = $('#foodcat :selected').val();
var dataString = "&ID=" + ID + "&check=";
$.ajax({
type: "POST",
url: "http://myurl.com/load_list.php",
data: dataString,
dataType: 'json',
crossDomain: true,
cache: false,
success: function (data) {
var result=$.parseJSON(data);
$.each(result, function (index, val) {
$("#foodlist").append($('<option></option>').val(val.Points).html(val.Name) + " - " + val.Quantity);
});
}
});
}
问题是它永远不会到达 success 函数: 这意味着如果我没有错,php 不会返回任何东西。
【问题讨论】:
-
请stop using
mysql_*functions。 These extensions 已在 PHP 7 中删除。了解PDO 和 MySQLi 的 prepared 语句并考虑使用 PDO,it's really pretty easy。 -
您在浏览器的开发者工具中查看过请求/响应吗?
-
在 php 端打印时给出了什么?
标签: php jquery mysql ajax cordova