2014-06-09 2 views
0

음식 항목 texbox에서 선택한 음식 항목을 기반으로 mysql 데이터베이스에서 값을 가져 와서 단가 텍스트 상자에 표시하려고합니다. Food item 텍스트 상자에 blur 이벤트를 사용하여 getJSON 메서드를 호출하고 DB에서 값을 가져 왔습니다. Firebug에서 getJSON으로부터 반환 된 응답을 볼 수 있지만 응답은 텍스트 상자에 표시되지 않습니다. 당신이 그것을 구문 분석이 필요하고, 그래서 코드는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 

아래 wriiten 내 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 결과가 문자열로 올 수 있습니다. 출력 형식을 변경해보십시오. – prava

+0

@AppleBud - unit_price 텍스트 상자 만 채우려고합니다. 문제는 unit_price와 만 같습니다. – Rams

답변

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

데이터는 JSON 개체,이다 다음 html 요소에 모든 가격을 지정하십시오 (예 :

01). 그 메시지는 하나 개의 항목이기 때문에 항상 false가 될 것입니다

$array[] = ""; 

if(json.length == 0) 때문에이에

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

:

2,는 당신의 PHP 스크립트와 변화를 편집 할 수 있습니다.

+0

코드를 구현했지만 잘못된 문자 오류가 발생했습니다. 경고 (가격)에 던져진다. – Rams

+0

$ .each() 함수에 대한 통찰력을 주신 You Cristian에게 감사드립니다. 제대로 작동했습니다. – Rams

관련 문제