我正在使用ajax创建html表,而不是我需要此表将一行一行保存在数据库中。我已经尝试了许多方法,但是我无法获得他所需的结果。您能否检查下面的代码,看看我的错误是什么。我正在向控制器获取数据,但不知道如何将其保存在数据库中。返回的不是我想要的实际消息吗?
// html页面
@extends('business.bakery_manager')
@section('tab-content')
<script type="text/javascript">
$(document).ready(function () {
var id = 1;
$(".add-row").click(function () {
var newid = id++;
$("#stocktable").append('<tr valign="top" id="' + newid + '">\n\
<td><input type=\'checkbox\' name=\'record\'></td>\n\
<td class="category' + newid + '">' + document.querySelector('#category option:checked').textContent + '</td>\n\
<td class="product' + newid + '">' + document.querySelector('#product option:checked').textContent + '</td>\n\
<td class="quantity' + newid + '">' + "<input type='text' id='qty' class='form-control' placeholder='Enter quantity here...'>" + '</td>\n\
<td class="product_id' + newid + '" style="display: none">' + $("#product").val() +'</td>\n\ </tr>');
});
// Find and remove selected table rows
$(".delete-row").click(function () {
$("table tbody").find('input[name="record"]').each(function () {
if ($(this).is(":checked")) {
$(this).parents("tr").remove();
}
});
});
});
</script>
<input type="hidden" name="products" id="products[]">
<div id="stock" class="active tab-pane">
@include('business.includes.messages')
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">To amend stock value, choose the product category and then choose the
product name.
Once these are found you can add or reduce stock levels.</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
{{--<form method="POST" action="{{action('StockController@ammendStock')}}" >
{{ csrf_field() }}--}}
<form>
<select id="category" class="col-lg-3 btn-lg">
<option style="display: none">Choose Product Category</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
<select name="product" id="product" class="col-lg-2 btn-lg"
style="margin-left: 10px; margin-right: 10px">
<option value="">
</option>
</select>
<select name="operator" id="operator" class="col-lg-2 btn-lg">
<option value="+">Increase</option>
<option value="-">Decrease</option>
</select>
<input type="button" class="add-row btn btn-lg bg-red-active col-lg-2 pull-right"
value="Add Row">
<table id="stocktable" class="table table-bordered table-striped table-responsive">
<thead>
<tr>
<th></th>
<th>Category</th>
<th>Product</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button type="button" class="delete-row btn btn-lg bg-orange col-lg-2">Delete Row</button>
<button id="buttonsave" class="btn btn-lg bg-aqua pull-right col-lg-2"> Save
</button>
</form>
</div>
<script>
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="_token"]').attr('content')
}
});
$('#category').on('change', function (e) {
console.log(e);
var category_id = e.target.value;
$.getJSON("/ajax-call?category_id=" + category_id, function (data) {
$('#product').empty();
$.each(data, function (ammend, productObj) {
$('#product').append('<option value="' + productObj.id + '">' + productObj.name + '</option>');
});
});
});
$("#buttonsave").click(function () {
var lastRowId = $('#stocktable tr:last').attr("id"); //finds id of the last row inside table
var product_name = new Array();
var product_id = new Array();
var quantity = new Array();
for (var i = 1; i <= lastRowId; i++) {
product_id.push($("#" + i + " .product_id" + i).html());
product_name.push($("#" + i + " .product" + i).html()); //pushing all the names listed in the table
quantity.push($("#" + i + " .quantity" + i ).html()); //pushing all the ages listed in the table
}
var sendproduct_id = JSON.stringify(product_id);
var sendproduct = JSON.stringify(product_name);
var sendquantity = JSON.stringify(quantity);
$.ajax({
url:'/savedata',
type: 'get',
data: {id: sendproduct_id, name: sendproduct, qty: sendquantity},
dataType: 'json',
success: function(data){ // What to do if we succeed
alert(data);
}
});
});
</script>
</div>
</div>
</div>
</div>
@停止
//控制器
public function ammendStock()
{
$product = json_decode(Input::get('name'));
$quantity = json_decode(Input::get('qty'));
$product_id = json_decode(Input::get('product_id'));
for ($i = 0; $i < count($product_id); $i++) {
if (($product_id[$i] != "")) { //not allowing empty values and the row which has been removed.
$stock = new Stock();
$stock->document_id = 1;
$stock->product_id = $product_id[$i];
$stock->quantity = 10;
$stock->save();
}
}
return back()->with('success','New Data');
}
///请您指导我如何获取数量,因为这是用户输入的信息,但我没有设法获取。
谢谢!