选择产品名称以从数据库自动填充价格

Selected product name to auto populate price from DB

我已经尝试了 中给出的解决方案,但仍然无法解决我的问题。 这是我的表格 (laravel blade):

    @extends('layouts.app')

    @section('content')
        <br>
        <h1>Add Sale</h1>
        {!! Form::open(['action' => 'SalesController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}

        <div class="form-group">
            <label for="product_name">Product Name</label>
            <select name="product_name" id="product_name" class="form-control">
                <option>Select Product Name</option>
                @foreach ($stocks as $stock)
                    <option value="{{ $stock->stock_name }}">{{ $stock->stock_name}}</option>
                @endforeach
            </select>
        </div>

        <div class="form-group">
            {{Form::label('sale_quantity', 'Quantity')}}
            {{Form::text('sale_quantity', '', ['class' => 'form-control', 'placeholder' => 'Quantity'])}}
        </div>

        <div class="form-group">
            <label for="unit_selling_price">Unit Selling Price </label>
            <select name="unit_selling_price" id="unit_selling_price" class="form-control">
                <option>Select Unit Selling Price</option>
                @foreach ($stocks as $stock)
                    <option value="{{ $stock->unit_selling_price }}">{{ $stock->unit_selling_price}}</option>
                @endforeach
            </select>
        </div>

        <div class="form-group">
            {{Form::label('total_sales_cost', 'Total Sales Cost')}}
            {{Form::text('total_sales_cost', '', ['class' => 'form-control', 'placeholder' => 'Total Sales Cost'])}}
        </div>
        {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
        {!! Form::close() !!}
    @endsection

以上表格截图。

您可以使用 ajax。您可以将 stock_name 作为路由参数或与 ajax 数据属性一起传递。对于这个例子,我将使用数据属性。

$("#product_name").on('change', function() {
    let element = $(this);
    $.ajax({
        url: 'your-url-here',
        method: 'GET',
        data: {
            'stock_name': element.val(),
        },
        success: function(response) {
            $("#unit_selling_price").val(response.data).trigger('change');
        },
    });
});

您的 PHP 脚本,在某些控制器方法中将如下所示:

public function getTotalCost(Request $request)
{
    $stock = Stock::where('stock_name', 'like', $request->input("stock_name"))->first();
    if ($stock == null) {
        return null;
    }
    return response()->json($stock->unit_selling_price);
}

希望对您有所帮助。

SalesController.php 变化

public function getUnitSellingPrice(Request $request, $stock_id)
{

    $stock = Stock::where('stock_id', $stock_id)->first();
    if ($stock == null) {
        return null;
    }

    return response()->json($stock->unit_selling_price);
}

create.blade.php 变化

<div class="form-group">
    <label>Product Name</label>
    <select name="product_name" class="form-control" id="stock_name">
        <option>Select Product Name</option>
        @foreach ($stocks as $stock)
            <option value="{{ $stock->stock_id }}">{{ $stock->stock_name}}</option>
        @endforeach
    </select>
</div>

<div class="form-group">
    {{Form::label('unit_selling_price', 'Unit Selling Price')}}
    {{Form::text('unit_selling_price', '', ['class' => 'form-control', 'placeholder' => 'Unit Selling Price', 'id' => 'unit_selling_price'])}}
</div>

<div class="form-group">
    {{Form::label('sale_quantity', 'Quantity')}}
    {{Form::text('sale_quantity', '', ['class' => 'form-control', 'placeholder' => 'Quantity', 'id' => 'sales_quantity'])}}
</div>

<div class="form-group">
    {{Form::label('total_sales_cost', 'Total Sales Cost')}}
    {{Form::text('total_sales_cost', '', ['class' => 'form-control', 'placeholder' => 'Total Sales Cost', 'id' => 'total_sales_cost', 'readonly' => 'true', 'cursor: pointer' => 'true' ])}}
</div>



{{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}

<script>
    $(document).ready(function () {
        $("#stock_name").on('change', function () {
            var stock_id = $(this).val();
            $.ajax({
                url: '/sales-price/getunitsellingprice/'+stock_id,
                method: 'GET',
                success: function (response) {
                    console.log(response);
                    $("#unit_selling_price").val(response);
                },
            });
        });
    });
</script>


<script>
    $(document).ready(function () {
        $("#total_sales_cost").click(function () {
            var sales_quantity = $("#sales_quantity").val();

            var unit_selling_price = $("#unit_selling_price").val();
            var total_sales_cost = (sales_quantity * unit_selling_price);

            $('#total_sales_cost').val(total_sales_cost);
        });
    });
</script>

路线变更

Route::any('sales-price/getunitsellingprice/{stock_id}','SalesController@getUnitSellingPrice');