Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Jquery populate text box with Json return

3:39 pm on Mar 18, 2013 (gmt 0)

New User

joined:Mar 13, 2013
posts: 3
votes: 0

I have a test form with two text boxes, the first text box allows me to type in a product id, and then the idea is that the second text box would be updated/display the item price, if I an get this working, I would add a few more text boxes to display a bit more info.When I use $('.results').html(data);as the success, I can display the data in a div named "result" what I am struggling with is to display one of the returned fields in a text box on the form.

This is a section of my form:

<script type="text/javascript">
var inpval = $('#inp').val();

type: "POST",
data: ({p : inpval}),
dataType: '',
success: function(data){
// $('#item_cost').html(new_data['product_id']);
$.each( data, function( key, value ) {
$( '#item_cost' + key ).val( 'item_cost' );


<form action="" method="post">
<label for="inp"></label>

<input type="text" name="inp" id="inp" />
<input name="item_cost" type="text" id="item_cost" />
<div class="results">Content for class "results" Goes Here


This is my query (no data cleansing shown for clarity):

<?php session_start();?>
<?php require_once('Connections/connpay.php'); ?>

$product_id = $_POST['p'];
$company_id = $_SESSION['sess_company_id'];

//* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());

$query = "SELECT * FROM acc_stock_items WHERE product_id = '$product_id' ";
if ($result = $conn->query($query)) {

/* fetch object array */
while ($row = $result->fetch_assoc()) {
$data = $row;
echo json_encode( $data );

and this is the Json being validated in jslint

"product_id": "1",
"product_ref": "",
"company_id": "5",
"product_name": "Plywood",
"category_id": "3",
"container_id": "2",
"product_description": "Plywood purchased in packs of 50 and sold individually.",
"purchase_cost": "1000",
"item_cost": "20",
"sales_price": "25.00",
"is_active": "1",
"stock_level": "56",
"min_stock_level": "0",
"date_added": "0000-00-00 00:00:00"
can anyone show me where it is I am going wrong in trying to display any one field in a text box on my form. David