从ajax返回值返回到输入字段

时间:2017-09-16 17:32:40

标签: php jquery ajax

如何将值数据插入ajax返回的每个字段输入?

  1. 这是我的输入字段

        <div class="form-group col-sm-2 ">
        <label>Barcode</label>
        <div class="input-group input-group-sm">
            <input type="text" class="form-control" name="inputBarcode" id="inputBarcode" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-info btn-flat"><i class="fa fa-search"></i></button>
            </span>                     
        </div>
    </div>
    <div class="form-group col-sm-4 input-group-sm">
        <label>Nama Barang</label>
        <input type="text" class="form-control" name="namaBarang" id="namaBarang" disabled/>
    </div>
    <div class="form-group col-sm-2 input-group-sm">
        <label>Satuan</label>
        <input type="text" class="form-control" name="satuanBarang" id="satuanBarang" disabled/>
    </div>
    <div class="form-group col-sm-2 input-group-sm">
        <label>Harga</label>
        <input type="text" class="form-control" name="hargaBarang" id="hargaBarang" disabled/>
    </div>
    <div class="form-group col-sm-2 ">
        <label>Jumlah</label>
        <div class="input-group input-group-sm">
            <input type="text" class="form-control" name="jumlahBarang" id="jumlahBarang" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-info btn-flat">Add</button>
            </span>                     
        </div>
    </div>
    
  2. 这是我的Ajax

    $('#inputBarcode').blur(function(){
    
        var noBarcode=$('#inputBarcode').val();
    
        $.ajax({
           type:'post',
           url:'dataBarang.php',
           data:{barcode:noBarcode},
    
           success:function(response){
               $('#namaBarang').val(response);
           },
    
        });
    });
    
  3. 这是来自ajax触发的查询

    if(isset($_POST['barcode'])){
    
        $barcode=$_POST['barcode'];
    
        $qry=mysqli_query($koneksi, "SELECT * FROM tbl_barang WHERE id_barang='$barcode' ");
    
        WHILE ($res=mysqli_fetch_assoc($qry)){
    
            $namaBarang=$res['nama_barang'];
            $hargaNormal=$res['harga_jual_barang'];
            $qtyDiskon=$res['qty_diskon'];
            $hargaDiskon=$res['harga_qty_diskon'];
    
        }
    }
    

1 个答案:

答案 0 :(得分:1)

从PHP返回JSON并在JS处解码并放置在INPUT FIELD

    if(isset($_POST['barcode'])){

$barcode=$_POST['barcode'];

$qry=mysqli_query($koneksi, "SELECT * FROM tbl_barang WHERE id_barang='$barcode' ");
$arr_data=array();
WHILE ($res=mysqli_fetch_assoc($qry)){

    $arr_data['namaBarang']=$res['nama_barang'];
    $arr_data['hargaNormal']=$res['harga_jual_barang'];
    $arr_data['qtyDiskon']=$res['qty_diskon'];
    $arr_data['hargaDiskon']=$res['harga_qty_diskon'];

}
echo json_encode($arr_data);
$('#inputBarcode').blur(function(){

    var noBarcode=$('#inputBarcode').val();

    $.ajax({
       type:'post',
       url:'dataBarang.php',
       data:{barcode:noBarcode},

       success:function(response){
           var obj=$.parseJSON(response);
           $('#namaBarang').val(obj.namaBarang);
       },

    });
});