Ich habe versucht, Kategorie Daten aus der Datenbank zu sammeln und in der Kategorie Dropdown-Liste in HTML zu zeigen. Es wird jedoch nicht funktionieren. Ich habe keine Ahnung, welcher Teil schief läuft. Die Ausgabe gibt null zurück und sammelt niemals Daten aus der Datenbankkategorietabelle.Datenbank Daten nie in HTML
Dies ist meine HTML-Datei
<!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);
?>
In addProduct.js, entfernen Sie http: // von http: //selectCategory.php? Callback =? – mulquin
Eigentlich ist dies ein Phonegap-Projekt, die php-Datei ist speichern auf Online-Server, der Speicherort mit der HTML-Datei ist –
OK, fügen Sie den Domänennamen oder IP-Adresse vor selectCategory.php – mulquin