如何在ajax中分离数组并将其分配给变量

时间:2017-08-09 10:57:01

标签: php jquery ajax laravel

我有为我的html获取整个数组的代码。

$('#btnSubmit').on('click', function () {
    array = [];
    $('.getOrders').each(function () {
        array = $(this).val();

        //i did console.log in here and i got the whole results//

        console.log(array);
    });
});

在我的控制器中:

$inc = $request->input('inc');
$inc2 = 1;
for ($inc2; $inc2 <= $inc; $inc2++) {
    $ix = ($request->input('i_name' . $inc2));
    $ix2 = ($request->input('quan' . $inc2));
    $ix3 = ($request->input('tot' . $inc2));
    $ix4 = ($request->input('id_categ' . $inc2));
    $ix5 = ($request->input('id_item' . $inc2));

    echo '<input type="hidden" name="order_name[]" id="order_name' . $inc2 . '" class="getOrders" value="' . $ix . '" disabled>';
    echo '<input type="hidden" name="item_quan[]" id="item_quan' . $inc2 . '" class="getOrders" value="' . $ix2 . '" disabled>';
    echo '<input type="hidden" name="price_tot[]" id="price_tot' . $inc2 . '" class="getOrders" value="' . $ix3 . '" disabled>';
    echo '<input type="hidden" name="id_categ[]" id="id_categ' . $inc2 . '" class="getOrders" value="' . $ix4 . '" disabled>';
    echo '<input type="hidden" name="id_item[]" id="id_item' . $inc2 . '" class="getOrders" value="' . $ix5 . '" disabled>';

}
echo '<input type="hidden" id="inc" value="' . $inc . '" disabled>';

return view('show_sales')->with([
    'name'   => $request->input('customer_name'),
    'or_no'  => $request->input('or_number'),
    'total'  => $request->input('getItemPrice'),
    'change' => $request->input('change'),
]);

如何切片整个数组并为其赋值,以便将其存储在数据库的特定列上?

1 个答案:

答案 0 :(得分:1)

首先问题是你的JS应该使用push()

$('#btnSubmit').on('click', function () {
    array = [];
    $('.getOrders').each(function (key,value) {
        array.push($(this).val());

    });
        console.log(array);

});

之后只需使用简单的Ajax来电并将array作为数据发送