簡體   English   中英

如何將html表的數據保存到Laravel的數據庫中

[英]How to save data from html table to database in Laravel

我使用JavaScript從文本字段向 html 表行輸入數據。 然后我需要將它們一一保存在一個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>

誰能舉個例子。

首先,您需要將要保存在數據庫中的所有輸入數據分組到一個指向 controller 方法的表單中。 然后 controller 將完成所有工作。

假設你在刀片文件中編寫前端: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查詢更改為您使用的 model。

示例: ItemInfo::create($request);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM