0

My route;

Route::post ('/sepetim/ajax', 'ShoppingCartController@addStock');

My Ajax code as below;

$.ajax({
    type: 'POST',
    url: '/sepetim/ajax',
    data: {
        '_token': $('input[name="_token"]').val(),
        'name': $('input[name=name]').val(),
        'cart_id': $('input[name=cart_id]').val(),
        'stock_id': id
    },
    success: function(data) {
        // alert(data.stock_id);
        $('#5').replaceWith("<span class='para fw6'>{{number_format(data.x * data.price, '2' , ',' , '.')}} TL</span>");

        // $('#u').replaceWith(" <p class='para toplamfiyat'>55,08 TL</p>");
    },
});
$('#name').val('');

My conroller as below;

public function addStock(Request $request) {
    $data = new ShoppingCartDetail();
    $data - > cart_id = $request - > input('cart_id');
    $data - > stock_id = $request - > input('stock_id');
    $data - > price = 1;
    $data - > save();
    $data - > x = 4;
    $data - > price = 1200;
    return response() - > json($data);
    //return response()->json(["data" => $data]);
}

I can get the values $data->price and $data->x in data but ajax success in part I can not print in.

ajax success function as below;

success: function(data) {
    // alert(data.stock_id);
    $('#5').replaceWith("<span class='para fw6'>{{number_format(data.x * data.price, '2' , ',' , '.')}} TL</span>");

    // $('#u').replaceWith(" <p class='para toplamfiyat'>55,08 TL</p>");
},

how can I pass data to this function in number_format()?

ufuk
  • 367
  • 3
  • 16
  • Number_format is php.you can't use like above .check this link for alternate solution.https://stackoverflow.com/questions/12820312/equivalent-to-php-function-number-format-in-jquery-javascript – NanThiyagan Jan 18 '18 at 10:46
  • @ufuk but be careful using `toFixed()`. this function may not always do what you want. – flx Jan 18 '18 at 10:49

3 Answers3

1

Instead of formatting your data in the view file, format your data in controller and then send it to view. So you can use the formatted data directly.

Change your controller code to following: public function addStock(Request $request) {

    $data = new ShoppingCartDetail();
    $data->cart_id = $request->input('cart_id');
    $data->stock_id = $request->input('stock_id');
    $data->price = 1;
    $data->save();
    $data->x = 4;
    $data->price = 1200;

    $formattedPrice = number_format($data->x * $data->price, '2' , ',', '.');

  return response()->json(["data" => $data, 'formattedPrice' => $formattedPrice]);
}

You can then use the new "formattedPrice":

$('#5').replaceWith("<span class='para fw6'>" + data.formattedPrice + "TL</span>");
QnARails
  • 377
  • 1
  • 4
  • 14
1
<html>
    <head>
        <title>Demo Run</title>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <form>
            <input type="checkbox" class="filter_sf js_filter" value="0-999" name="amount[]">1-100
            <input type="checkbox" class="filter_sf js_filter" value="1000-2000" name="amount[]">1001-2000
            <input type="checkbox" class="filter_sf js_filter" value="1000-2000" name="price[]">1001-2000
        </form>
    </body>
    <script type="text/javascript">
        $(document).on("change", ".js_filter", function () {
            var value = '';
            var ab_name = '';
            ab_name = $(this).attr('name');
            $('input[name^="' + ab_name + '"]:checkbox:checked').each(function () {
                value = value + $(this).val() + '--';
            });
            value = value.slice(0, -2);
            ab_name = ab_name.slice(0, -2)
            var filter_url = makeUrl(ab_name, value);
//            console.log(filter_url);
            top.location = filter_url;
        });
        function makeUrl(element, value) {
            var url = '';
            if (window.location.href.indexOf('?') >= 0) {
                url = window.location.href.substring(0, window.location.href.indexOf('?'));
                var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

                for (var i = 0; i < hashes.length; i++)
                {

                    hash = hashes[i].split('=');
                    if (hash[0] !== element) {
                        if (url.indexOf('?') === -1) {
                            url = url + "?";
                        } else {
                            url = url + "&";
                        }
                        url = url + hash[0] + "=" + hash[1];
                    }
                }

                if (value !== '') {

                    if (url.indexOf('?') === -1) {
                        url = url + "?";
                    } else {
                        url = url + "&";
                    }
                    url = url + element + "=" + value;

                }

            } else {
                url = window.location.href;
                if (value !== '') {
                    url = url + "?" + element + "=" + value;
                }
            }
            return url;
        }
    </script>
</html>
Sachin Aghera
  • 486
  • 3
  • 8
0

in your controller :

public function addStock(Request $request) {
$data = new ShoppingCartDetail();
$data-> cart_id = $request - > input('cart_id');
$data-> stock_id = $request - > input('stock_id');
$data-> price = 1;
$data-> save();
$data-> x = 4;
$data-> price = 1200;
$data->formattedPrice = number_format($data->x * $data->price, '2' , ',', '.');
return response()->json($data);
}

in your success function of ajax :

 success: function(response){
 console.log(response);

 var data=JSON.parse(response);
 console.log(data.formattedPrice);

  $('#5').replaceWith("<span class='para fw6'>"+formattedPrice+" TL</span>");

}
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • the part that does not work here -> number_format(data.x * data.price). not print values. – ufuk Jan 18 '18 at 10:56