参数从JavaScript传递到PHP

时间:2018-01-18 10:43:30

标签: javascript php ajax laravel-5

我的路线;

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

我的Ajax代码如下;

$.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('');

我的控制者如下;

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]);
}

我可以在数据中获取值$ data-&gt; price和$ data-&gt; x但是在第一部分中ajax成功无法打印。

ajax成功函数如下;

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

如何在number_format()?

中将数据传递给此函数

3 个答案:

答案 0 :(得分:1)

不是在视图文件中格式化数据,而是在控制器中格式化数据,然后将其发送到视图。因此,您可以直接使用格式化数据。

将您的控制器代码更改为以下内容: 公共功能addStock(请求$ 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]);
}

然后,您可以使用新的&#34; formattedPrice&#34;:

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

答案 1 :(得分: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>

答案 2 :(得分:0)

在您的控制器中:

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

在你的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>");

}