2017-07-18 80 views
0

我試圖從數據庫中收集類別數據並在html中的類別下拉列表中顯示。但是,它不會工作。我不知道哪個部分出了問題。輸出返回null,它從不收集數據庫類別表中的任何數據。數據庫數據永遠不會顯示在html中

這是我的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); 
?> 
+1

在addProduct.js中,從http://selectCategory.php中刪除http:// callback =? – mulquin

+0

其實這是一個phonegap項目,php文件是存儲在線服務器,這是與html文件不同的位置 –

+0

好吧,添加域名或IP地址之前selectCategory.php – mulquin

回答

0

您正在使用mysql_connectmysql_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); 
?> 
+0

使用此代碼,selectCategory.php已經向我返回了正確的數據,但它仍然不會顯示在HTML文件中 –

+0

有兩件事:在標記中,您的身體有兩次。在您的$ .ajax中,將類型更改爲'get' – mulquin

+0

我已經刪除了該主體。我改變類型也得到,但它也不會顯示 –

0

在selectCategory.php文件,你有以下部分:

$.ajax({ 
    url: 'http://selectCategory.php?callback=?', 

你在你的請求,這應該是一個不同的域的呼叫,而你的情況是不是也該回調參數實際上心不是做任何事情有HTTP,嘗試改變它像這樣:

$.ajax({ 
    url: '/selectCategory.php', 
+0

其實這是一個phonegap項目,PHP文件是存儲在線服務器,這是不同的位置與HTML文件 –

+0

然後,你必須把你的在線服務器的位置ajax調用 – NickyP

+0

是的,我做了,我也刪除了回調參數,但是當我直接去selectCategory.php的位置,它返回我null –