nishanthi nishanthi - 5 months ago 22
Ajax Question

How to pass bootstrap modal values to laravel controller using ajax?

I have created a bootstrap modal with some input fields and I am trying to pass those input values to controller and need to insert into db.But it displays null.

It will be helpful if anyone solves my issue.

Answer Source

You just need to change the JS code and laravel redirection with JSON format data like answered by Don't Panic. Added here for other people help so credit goes to him.

JS code:

$("#priceSave").click(function(e){
    e.preventDefault();
    var data = $('form').serialize();
    $.ajax({
        url: 'addPriceDetails/{{$dataId}}',
        type: "post",
        data: data,
        dataType: 'json',
        success: function(response) {
            alert(response.SKUID);
        }
    });
});

Controller code:

public function addPriceDetails(Request $priceform,$dataId) {
    // ... all your code
    return response()->json([
        'SKUID'    => $priceInfo->SKUID,
        'listingStatus' => $priceInfo->listingStatus
        // ... any other fields you want to return
    ]);