我尝试从数据库中收集类别数据,并在html的类别下拉列表中显示。但是,它不会起作用。我不知道哪个部分出了问题。输出返回null,它从不从数据库类别表中收集任何数据。
这是我的html文件
<!DOCTYPE html>
<html>
<style>
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
</style>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/ionic.min.css" />
<title>Create an Account</title>
</head>
<body onload="selectCategory()">
<div class="bar bar-header">
<a href="index.html" class="button button-clear button-royal">Back</a>
<h1 class="title">Add an Item</h1>
</div>
<div class="padding" style="margin-top:75px;">
<label class="item-input">
<span class="input-label">Item Name</span>
<input type="text" placeholder="" id="itemName">
</label>
<label class="item-input">
<span class="input-label">Category</span>
<select id="select_category">
<option value="0">- Select -</option>
</select>
</label>
<label class="item-input">
<button class="button button-block button-positive" id="signup">Add item</button>
</label>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jq.js"></script>
<script type="text/javascript" src="js/addProduct.js"></script>
</body>
</html>
addProduct.js
function selectCategory() {
$.ajax({
url: 'http://selectCategory.php?callback=?',
type: 'post',
data: "",
dataType: 'json',
success:function(response){
var len = response.length;
$("#select_category").empty();
for( var i = 0; i<len; i++){
var id = response[i]['id'];
var name = response[i]['name'];
$("#select_category").append("<option value='"+id+"'>"+name+"</option>");
}
}
});
}
selectCategory.php
<?php
header("Access-Control-Allow-Origin: *");
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$stmt = $pdo->query("SELECT * FROM category");
$users_arr = array();
while ($row = $stmt->fetch()) {
$users_arr[] = array("id" => $row['id'], "name" => $row['name']);
}
// encoding array to json format
echo json_encode($users_arr);
?>
答案 0 :(得分:0)
在selectCategory.php文件中,您有以下部分:
$.ajax({
url: 'http://selectCategory.php?callback=?',
你的请求中有HTTP,这应该是对不同域的调用,在你的情况下不是,回调参数实际上也没有做任何事情,尝试改变它:
$.ajax({
url: '/selectCategory.php',
答案 1 :(得分:0)
您正在使用已弃用的mysql_connect
和mysql_select_db
。请改用PDO访问您的数据库:
<?php
header("Access-Control-Allow-Origin: *");
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$stmt = $pdo->query("SELECT * FROM category");
$users_arr = array();
while ($row = $stmt->fetch()) {
$users_arr[] = array("id" => $row['id'], "name" => $row['name']);
}
// encoding array to json format
echo json_encode($users_arr);
?>