php - auto assign dynamic textbox with value from database based on typed word in another dynamic text box -


i have dynamic inputs first 1 under 'name' column, fetching data mysql , displaying suggestions user types (am making use of bootstrap typeahead). problem comes in when value typed user matches database, cost of respective item must displayed under 'unit cost' column. here html table.

<table class="table table-bordered table-hover" id="make_sales_table">                 <thead>                     <tr class="bg-gray">                         <th>no.</th>                         <th>name</th>                         <th>qty</th>                         <th>unit cost</th>                         <th>total cost</th>                     </tr>                   </thead>                   <tbody>                 <?php                          $d_q    =   $conn->prepare('select * products_tbl order prod_name asc');                         $d_q->execute();                         $d_f = $d_q->fetchall();                 ?>                       <tr id='add_row0'>                         <td>1</td>                         <td>                                     <!--                             <select name="input_item_name" id="input_item_name" class="form-control input_item_class_name" required>                                 <option selected disabled>---select---</option>                                 <?php foreach($d_f $row){?>                                 <option value="<?php echo $row['prod_code'];?>"><?php echo $row['prod_name'];?></option>                                 <?php } ?>                             </select>                             -->                             <input type="text" class="form-control input_item_name_class" id="input_item_name_id" name="input_item_name" placeholder="enter product name" required>                         </td>                         <td>                             <input type="text" class="form-control input_qty_class" name="input_item_qty" id="input_item_qty" placeholder="enter quantity"  onkeypress="return isnumeric(event);" required>                         </td>                         <td>                             <input type="text" class="form-control" name="input_item_cost" id="input_item_cost" placeholder="unit cost of item" value="0" readonly required>                         </td>                         <td>                             <input type="text" class="form-control" name="input_item_total_cost" id="input_item_total_cost" placeholder="total cost of item" value="0" readonly required>                         </td>                       </tr>                          <tr id='add_row1'>                         </tr>                   </tbody>                   <tfoot>                     <tr>                         <th colspan="4" class="text-center">total</th>                           <th>                             <input type="text" value="50000" class="form-control" name="input_final_total_cost" id="input_final_total_cost">                         </th>                     </tr>                   </tfoot>                    </table>                  <table class="table table-bordered no-border">                   <tbody>                     <tr>                         <td>                             <button type="button" id="add_row" class="btn btn-xs bg-olive" title="add item"><i class="fa fa-plus"></i></button>                              &nbsp;|&nbsp;                              <button type="button" id="delete_row" class="btn btn-xs bg-red" title="remove added item"><i class="fa fa-minus"></i></button>                         </td>                       </tr>                   </tbody>               </table> 

here script adds new text boxes.

<script>         $(document).ready(function () {      var = 1;     $("#add_row").click(function () {        var td =  $('#add_row' + i).html("<td>" + (i + 1) + "</td><td><input type='text' class='form-control input_item_name_class' id='input_item_name_id' name='input_item_name[]"+i+         "' placeholder='enter product name' required></td><td><input type='text' class='form-control input_qty_class' name='input_item_qty[]"+i+ "' id='input_item_qty' placeholder='enter quantity' onkeypress='return isnumeric(event);' required></td><td><input type='text' class='form-control input_item_cost_class' name='input_added_item_cost[]"+i+ "' id='input_added_item_cost[]' placeholder='unit cost of item' value='0' readonly required></td><td><input type='text' class='form-control' name='input_item_total_cost[]"+i+ "' id='input_item_total_cost' placeholder='total cost of item' value='0' readonly required></td>");        $('#make_sales_table').append('<tr id="add_row' + (i + 1) + '"></tr>');          td.find('input.input_item_name_class').typeahead({                ajax: {                         url: 'views/get_products_data.php',                         method: 'post',                         datatype:'json',                         triggerlength: 1                     },                     onselect: displayresult1          });         i++;          function displayresult1(item) {                  }      });     $("#delete_row").click(function () {         if (i > 1) {             $("#add_row" + (i - 1)).html('');             i--;         }     });   });     $(document).ready(function () {     function displayresult(item) {              }     $('#input_item_name_id').typeahead({                  ajax: {                     url: 'views/get_products_data.php',                     method: 'post',                     datatype:'json',                     triggerlength: 1                 },                 onselect: displayresult         });});     </script> 

here get_products_data.php page generating json.

<?php       $conn =new mysqli('localhost', 'root', 'jjtryhncr2nor2s6' , 'pos');      $sql = $conn->prepare("select prod_name products_tbl");     $sql->execute();     $result = $sql->get_result();  if ($result->num_rows > 0) {          while($row = $result->fetch_assoc()) {         $productresult[] = $row["prod_name"];         }         echo json_encode($productresult);     }     $conn->close(); ?> 

here script fires-up events unit cost of item.

$(document).ready(function(){      $("#input_item_name_id").on({         keyup:function(){          var product_code    =   $("#input_item_name_id").val();                  $.post("_action.php",{             product_key: product_code         }, function(data){             $("#input_item_cost").val(data);         }               );         },         focus:function(){         var product_code    =   $("#input_item_name_id").val();                  $.post("_action.php",{             product_key: product_code         }, function(data){             $("#input_item_cost").val(data);         }               );         }      });   }); 

finally here _action.php fetching unit price mysql database.

<?php  if(isset($_post['product_key']) && !empty($_post['product_key'])){      $product_key    =   $_post['product_key'];      $pk_q   =   $conn->prepare("select prod_unit_cost products_tbl prod_name='$product_key'");     $pk_q->execute();     $pk = $pk_q->fetchobject();      if($pk){          echo $pk->prod_unit_cost;     }     else{          echo 0;     }   }//isset //post ?> 

thanks in advance.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -