2014-06-09 75 views
0

我想從基於Food item texbox中選擇的食品項目的mysql數據庫中提取值,並將其顯示在單元價格文本框中。我在Food item文本框中使用了blur事件來調用getJSON方法並從數據庫中獲取值。 我可以在Firebug中看到getJSON返回的響應,但響應不顯示在文本框中。該代碼wriiten下面Json響應沒有顯示文本框中的值

<div class='col-sm-3'>  
    <div class='form-group'> 
    <label>Food Item</label> 
     <input class="form-control" id="item_name" name="itemname" autocomplete="off" size="30" type="text" required="true"/> 
    </div> 
</div> 
<div class="col-md-1"></div> 
<div class='col-sm-1'>  
    <div class='form-group'> 
    <label>Quantity</label> 
    <input class="form-control" id="quantity" name="quantity" autocomplete="off"  type="number" min="1" max="100" required="true"/> 
    </div> 
</div> 
<div class="col-md-1"></div> 
<div class='col-sm-1'>  
    <div class='form-group'> 
    <label>Unit price </label> 
    <input class="form-control" id="unit_price" name="unitfoodprice" type="number" /> 
    </div> 
</div> 

<div class="col-md-1"></div> 
    <div class='col-md-1'> 
    <div class='form-group'> 
     <br> 
     <button type="button" id="additems" name="additems" class="btn btn-sm btn-primary">Add Items</button> 
    </div> 
    </div>              
    </div 

我jQuery代碼是

//To get the food item price 
    $(document).ready(function() { 

     $('#item_name').blur(function() { 

      if( $("#item_name").val() !== "") 
     { 
      //To pass the customer details and fetch related details 
      $.getJSON("getfooditemprice.php", {fooditemname: $('#item_name').val()}, function(data){ 

          if(data === "empty") 
          { 
           //$('#myModal').hide(); 
           alert('No Price exists for the particular Food item');       
           $("#item_name").val(''); 
           $("#item_name").focus(); 
           return false;       
          } 
          var foodprice = data['price']; 
          $('#unit_price').val(foodprice); 

        });     

     } 

     }); 
}); 

我的PHP代碼是

require 'core/init.php'; 

if (isset($_REQUEST['fooditemname'])) { 

$query = $_REQUEST['fooditemname']; 
    $sel_fooditemprice = "SELECT price FROM fooddetails WHERE itemname = '$query'"; 
    $stmt = $db->prepare($sel_fooditemprice); 
    $stmt->execute(); 

    //this is how to get number of rows returned 
    $num = $stmt->rowCount(); 
    if ($num) 
    { 
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
     $array[] = $row['price']; 
     } 
     echo json_encode ($array); //Return the JSON Array 
    } 
    else 
    { 
     $array[] = "No Price exists for this food item"; 
     echo json_encode($array); 
    } 
} 
+0

問題僅限於unit_price或所有文本框? – AppleBud

+0

正如您所提供的,unit_price'textbox'爲'number',並且json結果可能以字符串形式出現。請嘗試更改輸出類型ti(int)' – prava

+0

@AppleBud - 我試圖只填寫unit_price文本框,並且問題僅適用於unit_price – Rams

回答

1
var foodprice = data['price']; 
$('#unit_price').val(foodprice); 

數據是一個JSON對象,所以你需要分析它,並然後將每個價格分配給一些html元素,例如:

$.getJSON("getfooditemprice.php", {fooditemname: $('#item_name').val()}, function(json){ 
    if(json.length == 0){ //check if is data returned 
     //$('#myModal').hide(); 
     alert('No Price exists for the particular Food item'); 
     $("#item_name").val(''); 
     $("#item_name").focus();    
    }else{ 
     $.each(json, function(i, item) { 
      price = item; 
      //do sometinh with price 
      //example 
      alert(price); 
     });​ 
    } 
});  

然後編輯你的PHP腳本和變化:

$array[] = "No Price exists for this food item"; 

這樣:

$array[] = ""; 

這是因爲if(json.length == 0)將永遠假的,因爲該消息是一個項目。

+0

實現你的代碼,但是非法字符錯誤在其他構造中被拋出alert(price) – Rams

+0

感謝YOu Cristian對$ .each()函數的洞察。正常工作。 – Rams