javascript - 如何在 Laravel 中将数据从 html 表保存到数据库
问题描述
我通过使用将文本字段中的数据输入到 html 表格行JavaScript
。然后我需要将它们mysql
一一保存在一个表中。所以我想知道如何将它们保存到数据库中。我把代码放在下面。
function addRow() {
var table = document.getElementById("tbody");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var items = document.getElementById("item").value;
var suppliers = document.getElementById("supplier").value;
var quantities = document.getElementById("quantity").value;
var grnprices = document.getElementById("grnprice").value;
if (items == "", suppliers == "", quantities == "", grnprices == "") {
alert("Please fill the Required Field");
} else {
var values = parseInt(document.getElementById('rawno').value, 10);
values = isNaN(values) ? 0 : values;
values++;
document.getElementById('rawno').value = values;
var total = quantities * grnprices;
row.insertCell(0).innerHTML = values;
row.insertCell(1).innerHTML = items;
row.insertCell(2).innerHTML = suppliers;
row.insertCell(3).innerHTML = quantities;
row.insertCell(4).innerHTML = "Rs. " + grnprices;
row.insertCell(5).innerHTML = "Rs. " + total;
row.insertCell(6).innerHTML = '<button type ="button" class="btn btn-danger" onClick="Javacsript:deleteRow(this)"> <i class="fa fa-trash-o"></i></button>';
}
}
<table class="table table-striped">
<thead>
<tr>
<th scope="col" width="200">Item</th>
<th scope="col" width="200">Supplier</th>
<th scope="col" width="200">Quantity</th>
<th scope="col" width="200">GRN Price</th>
<th scope="col" width="50"></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" min="0" class="form-control" name="item" id="item">
</td>
<td>
<input type="number" min="0" class="form-control" name="supplier" id="supplier">
</td>
<td>
<input type="number" min="1" class="form-control" name="quantity" id="quantity">
</td>
<td>
<input type="number" min="0" class="form-control" name="grnprice" id="grnprice">
</td>
<td>
<input type="button" class="btn btn-info" id="add" value="Add" onclick="Javascript:addRow()">
</td>
<td>
<input type="hidden" name="rawno" id="rawno">
</td>
</tr>
</tbody>
</table>
</div>
<table class="table" id="myTableData">
<thead>
<tr>
<th>No</th>
<th>Item</th>
<th>Supplier</th>
<th>Quantity</th>
<th>GRN Price</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody id="tbody">
<tr></tr>
</tbody>
</table>
<button type="button" class="btn btn-success" onclick="grnConfirmation()">Save</button>
谁能举个例子。
解决方案
首先,您需要将所有要保存在数据库中的输入数据分组到一个指向控制器方法的表单中。然后控制器将完成所有工作。
假设您在刀片文件中编写前端:create.blade.php
<form method="POST" action="ItemInfoController@store">
<input type="text" min="0" class="form-control" name="item" id="item">
<input type="number" min="0" class="form-control" name="supplier" id="supplier">
<input type="number" min="1" class="form-control" name="quantity" id="quantity">
<input type="number" min="0" class="form-control" name="grnprice" id="grnprice">
<input type="submit" value="submit">
</form>
在 ItemInfoController.php 中:
public function store(Request $request) {
// This will add the data from input to the database.
// You can change the query builder as you need.
DB::table('item')->insert(['item' => $request->item,
'supplier' => $request->supplier,
'quantity' => $request->quantity]);
}
如果您想使用 Eloquent,您可以使用DB
您使用的模型更改查询。
例子 :ItemInfo::create($request);
推荐阅读
- multithreading - 处理 std::thread c++ 中的泄漏
- post - 捕获带有乱码正文的 HTTP Post 请求
- javascript - 如何映射对象数组列表以显示在日历中?
- python - 插值问题 - ValueError:所需数组的深度太小(Python,numpy)
- javascript - 输入 event.target.value 不像输入中的字符串(类型号)
- .net-core - 在 Visual Studio 2019 中针对 WSL 中的文件夹进行开发
- python - 如何在自己的 Python 包中结合自己的 C 扩展
- node.js - 即使 source_id 是字符串,也会为 source_id 获取字符串预期错误(使用带有云函数的方形连接)
- csv - 如何在 Apache Nifi 中加入两个或多个 CSV 文件或如何将列复制到另一个 CSV 文件
- android - 在某些片段中隐藏导航抽屉