简体   繁体   中英

I want to populate form input based on another input field

I am developing this where I enter enter the name of the product and the details would display in other input fields. I am able to use jquery autocomplete select the product name.

Below is my code. Please what is really wrong with my code since it works once and stop.

My HTML

<label for="inputEmail3" class="col-sm-2 control-label">Product Name</label>
<div class="col-sm-4 ui-widget">
<input type="text" class="form-control" placeholder="Name of Product" name="search_prod_name" id="search_prod_name">
<div id="product_List">
</div>
</div>

JQUERY

<script> 
$(document).ready(function(){

    $('#search_prod_name').change(function() {
        var prodd_name = $(this).val();
        $.ajax({
                url:"select_for_sell.php",
                method: "POST",
                data:{prodd_name: prodd_name},
                dataType: "JSON",

                success:function(data) {
                    $('#pro_cat').val(data.pro_cat);
                    $('#product_name').val(data.product_name);
                    $('#manu_name').val(data.manu_name);
                    $('#supp_name').val(data.supp_name);
                    $('#unit_sell_price').val(data.unit_sell_price);
                    $('#manu_dt').val(data.manu_dt);
                    $('#expiry_dt').val(data.expiry_dt);
                    $('#qty_in_stock').val(data.qty);
                }
        }); 
    }); 
}); 
</script> 

My PHP

<?php 

include_once('conn/conn.php');
if(isset($_POST["prodd_name"])) {
    $query_product = "SELECT * FROM sellnsell_products WHERE product_name = '". $_POST["prodd_name"]."' ";
    $result_product = mysqli_query($cnn, $query_product);
    while($rrow = mysqli_fetch_array($result_product)) {
        $data["pro_cat"]            = $rrow["product_cat"];
        $data["product_name"]       = $rrow["product_name"];
        $data["manu_name"]      = $rrow["manu_name"];
        $data["supp_name"]      = $rrow["supp_name"];
        $data["unit_sell_price"]    = $rrow["unit_sell_price"];
        $data["qty_in_stock"]   = $rrow["qty"];
        $data["manu_dt"]            = $rrow["manu_dt"];
        $data["expiry_dt"]      = $rrow["expiry_dt"];

    }
    echo json_encode($data);
}
?>

Try this change this,

$('#search_prod_name').change(function() {

to this,

$('#search_prod_name').keydown(function() {

also change your SQL query to use prepared statements using variables directly inside mySQL query opens up the system to be attacked using a MySQL injection method.

Also remove those unnecessary spaces in your PHP just keep it simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM