| Jquery populate text box with Json return
|
upshire

msg:4556099 | 3:39 pm on Mar 18, 2013 (gmt 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"> $(function(){ $('#inp').keyup(function(){ var inpval = $('#inp').val();
$.ajax({ type: "POST", data: ({p : inpval}), dataType: '', url:"ajax_product_list.php", success: function(data){ //$("#item_cost").val(data[0].item_cost); //alert(data); //$('.results').html(data); // $('#item_cost').html(new_data['product_id']); //$("#item_cost").val(data[1].product_id); $.each( data, function( key, value ) { $( '#item_cost' + key ).val( 'item_cost' ); }); } }); }); });
</head>
<body> <form action="" method="post"> <label for="inp"></label>
<input type="text" name="inp" id="inp" /> <input name="item_cost" type="text" id="item_cost" /> </form> <div class="results">Content for class "results" Goes Here
</div> This is my query (no data cleansing shown for clarity): <?php session_start();?> <?php require_once('Connections/connpay.php'); ?> <?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()); exit(); } $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 ); } } $result->free(); $conn->close(); ?> 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
|
|