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()?

2
  • Number_format is php.you can't use like above .check this link for alternate solution.stackoverflow.com/questions/12820312/… Commented Jan 18, 2018 at 10:46
  • @ufuk but be careful using toFixed(). this function may not always do what you want. Commented Jan 18, 2018 at 10:49

3 Answers 3

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>");
Sign up to request clarification or add additional context in comments.

1 Comment

Yes here's the solution I'm looking for. You're super :)
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>

Comments

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>");

}

1 Comment

the part that does not work here -> number_format(data.x * data.price). not print values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.