음식 항목 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);
}
}
unit_price 또는 모든 텍스트 상자에만 문제가 있습니까? – AppleBud
제공 한대로 unit_price'textbox'를'number'로 지정하면 json 결과가 문자열로 올 수 있습니다. 출력 형식을 변경해보십시오. – prava
@AppleBud - unit_price 텍스트 상자 만 채우려고합니다. 문제는 unit_price와 만 같습니다. – Rams