【发布时间】:2020-01-28 04:11:41
【问题描述】:
我创建了一个 html 表单,用户可以自己添加额外的行。链接中的网站图片。 html form site
每次用户添加新行时,都会为输入的每一行生成一个新名称。 例如。第 1 行输入名称将是:itemid0,amount0。第 2 行输入名称将是 item1,amount1。等等。下面是html表单的代码。
<?php
include('session.php');
?>
<?php
$ItemID = "ItemID";
$ItemName = "ItemName";
$UnitPrice = "UnitPrice";
$sql = "SELECT ItemID,ItemName,UnitPrice FROM Item";
$result = $db->query($sql);
?>
<html>
<head>
<title>Add Sales</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="text-center">
<h1>Add new Sales Record</h1>
</div>
<div class="container">
<a href="sales.php"><button class="w3-button w3-black w3-round-large">Back</button></a>
</div>
<div class="container" align="right">
<table class="table-sm">
<tbody>
<tr class="table-primar">
<th scope="col" style="padding-right: 50px;">ItemID</th>
<th scope="col" style="padding-right: 50px">ItemName</th>
<th scope="col">UnitPrice</th>
</tr>
<?php
while($rows = $result->fetch_assoc()) {
?>
<tr class="contents">
<td><?php echo $rows[$ItemID]; ?></td>
<td><?php echo $rows[$ItemName]; ?></td>
<td><?php echo $rows[$UnitPrice]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<hr>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
ItemID
</th>
<th class="text-center">
Amount
</th>
</tr>
</thead>
<tbody>
<form action="addsales.php" method="POST">
<tr id='addr0'>
<td>
<input type="text" name='itemid' placeholder='ItemID' class="form-control"/>
</select>
</td>
<td>
<input type="text" name='amount' placeholder='Amount' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</form>
</tbody>
</table>
</div>
</div>
<button id="add_row" class="w3-button w3-black w3-round-large" style="position: absolute; right: 130px;">New row</button>
<button class="w3-button w3-black w3-round-large">Save</button>
</div>
<script type="text/javascript">
$(document).ready(function() {
var i = 1;
$("#add_row").click(function() {
$('#addr'+(i-1)).find('input').attr('disabled',true);
$('#addr' + i).html("<td><input name='itemid" + i + "' placeholder='ItemID' class='form-control input-md'/></td><td><input type='text' name='amount" + i + "' placeholder='Amount' class='form-control input-md'/></td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
i++;
});
});
</script>
</body>
</html>
现在的问题是,我不知道如何调用存储在表单每一行中的每个单独的信息。我在下面包含了另一个 sn-p。这就是我用于另一种只接受一行输入的表单。
<?php
include('session.php');
$itemname = trim($_POST['itemname']);
$unitprice = trim($_POST['unitprice']);
$qty = trim($_POST['qty']);
$supplierid = trim($_POST['supplierid']);
$sql = "INSERT INTO item (ItemName,UnitPrice,QtyStock,SupplierID) VALUES ('{$itemname}', '{$unitprice}', '{$qty}', '{$supplierid}')";
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Add Item</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="asset/css/bootstrap.css" rel="stylesheet"/>
<script src="asset/js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<table width="100%" cellpadding="5" cellspacing="5">
<tr>
<td>
<h2>Adding Item</h2>
</td>
</tr>
<tr>
<td style="text-align: center;">
<?php
// If insert data successfully, show information and redirect to index.php
if($db->query($sql) == TRUE){
echo "<div id='message'>New record created successfully <hr/>Please wait to redirect...</div>";
echo "<meta http-equiv='refresh' content='10;url=stocks.php'>";
} else {
echo "<div id='message'>Error: " . $sql . "<br>" . $db->error ."<hr/>Please wait to redirect...</div>";
echo "<meta http-equiv='refresh' content='10;url=stocks.php'>";
}
?>
</td>
</tr>
</table>
</div>
</div>
<?php $db->close(); ?>
</div>
</body>
</html>
我想你会使用类似的东西,但以某种方式遍历所有行?
【问题讨论】:
标签: javascript php html sql