Robert_Wlliams's avatar

I am trying get price of specific product using ajax call. so far i had tried this.

my blade.php

<select name="productname[]" class="form-control productname">
   <option value="0" selected="true" disabled="true">Select Product</option>
   @foreach ($stock as $product)
   <option value="{{$product->id}}">{{$product->productname}}</option>
   @endforeach
   </select>

user select product from here and i want ptice input feild auto-filled.

<td><input type="text" name="price[]" class="form-control price"></td>

using this javascript

 $('tbody').delegate('.productname','change', function(){
    var tr=$(this).parent().parent();
    var id = tr.find('.productname').val();
    var dataId={'id':id};
    $.ajax({
        type    :   'GET',
        url     : "{{route('findprice')}}",
        dataType:   'json',
        data    :   dataId,
        success:function(data){
            tr.find('text.price').val(data[0].price);
        }
    });
});

and in my controller I am using this thing.

public function findprice(Request $request)
{
    $data = Stock::where('price')->where('id',$request->id)->first();
    return response()->json($data);
}

anyone who can help me out.

0 likes
8 replies
Nakov's avatar

@robert_wlliams you can start by debugging, so check your browser Network tab on what is the response from your request.. Is it a correct one?

You can also print to the console, in the success callback: console.log(data); and check what does that returns..

And third:

    $data = Stock::where('price')->where('id',$request->id)->first();

is a wrong query, you have where('price') checking for no value at all, so remove that:

    $data = Stock::where('id',$request->id)->first();
Snapey's avatar

Why not send the price with the view in the first place? You seem to be making things complicated for yourself?

Robert_Wlliams's avatar

there are multiple products and how can i call price in view. if there are hundreds of product user will be searching for the correct price then

Robert_Wlliams's avatar

@nakov getting product id right but nt gettimg price there. only cally function name. like this findprice?id

Nakov's avatar

@robert_wlliams is the id that you are passing to the request an id of the product, or a Stock id?

Do you maybe need to look for this instead:

$data = Stock::where('product_id', $request->id)->first();

What is the result in your Network tab, or the console using console.log(data);?

Robert_Wlliams's avatar

@nakov Stock id and product id both are same. I tried all the 3 things suggested by you results are same. and by using condole.log(data); other scripts are not working.

Nakov's avatar

@robert_wlliams not condole but console and that just prints output to the Browser Console, it will not stop any other script.. You should share exact errors that you get, and exact code that you are changing. I cannot help you like this.

jlrdw's avatar

What is currently being returned in the response.

After re-reading four times, I highly suggest you get all this working with correct results without jQuery that way you know it's working then Implement jQuery.

It's always best to test first anyway because if you have incorrect queries but yet you're trying to troubleshoot jQuery. Just get the queries returning correct results first.

Please or to participate in this conversation.