Javascript 多个未定义的数量输入
好的,我正在创建一个小系统,以便客户可以在线向我们下订单。我们将通过电子邮件接收订单,并在我们自己的系统中进行处理。他们会在页面上下订单,并填写一些基本信息。之后,他们需要填写订单。每个订单行有3个字段。零件号、数量和价格。价格将是只读的,不会提交。只显示价格 我想用jQuery创建一个小的输入字段和一个加号图标。当您键入10并单击加号图标时,它们将有10行额外的3个字段。这个我还没有创造出来,但现在你知道我想要如何工作了 问题是我如何处理表格?字段是必需的,但我想当一行是空的,它不会被提交或在PHP忽略。所以我只想检查已经填写的表格。有时是4行,有时是20行,有时是100行。所以我们不知道输入的数量。我应该如何创建表单名称 以下是一些PHP:Javascript 多个未定义的数量输入,javascript,php,jquery,html,Javascript,Php,Jquery,Html,好的,我正在创建一个小系统,以便客户可以在线向我们下订单。我们将通过电子邮件接收订单,并在我们自己的系统中进行处理。他们会在页面上下订单,并填写一些基本信息。之后,他们需要填写订单。每个订单行有3个字段。零件号、数量和价格。价格将是只读的,不会提交。只显示价格 我想用jQuery创建一个小的输入字段和一个加号图标。当您键入10并单击加号图标时,它们将有10行额外的3个字段。这个我还没有创造出来,但现在你知道我想要如何工作了 问题是我如何处理表格?字段是必需的,但我想当一行是空的,它不会被提交或在
if(toxInput::exists()){
if(toxToken::check(toxInput::get('token')){
}
}
下面是表单外观的屏幕截图:
以下是表单的HTML:
<p><h2>Items</h2></p>
<p class="order">
<label style="margin-left:20px;">Partnumber:</label><br />
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
</p>
<p class="order" >
<label style="margin-left:20px">Qty</label><br>
<input type="text" id="qty" name="qty" style="margin-left:20px;" class="text small" placeholder="Qty" />
<p class="order">
<label style="margin-left:20px">Price</label><br>
<input type="text" style="margin-left:20px;" class="text small" placeholder="Price" readonly /><br>
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<p>
<input type="text" style="margin-left:20px;" class="text medium" placeholder="SP partnumber" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Qty" />
<input type="text" style="margin-left:16px;" class="text small" placeholder="Price" readonly />
</p>
<input type="hidden" name="token" value="<?php echo toxToken::generate(); ?>">
项目
零件号:
数量
价格
我写这篇文章是为了向您展示如何附加输入字段,我将使用数组来实现。我只使用一个输入变量(myOrder),但您可以使用任意多个。在for循环中更容易验证
<html>
<head>
<script>
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#myButton").click(function(){
// create loop to add ten inputs if needed
$("#myOrderForm").append('Product: <input type="text"
name="myOrder[]" /><br>');
});
});
</script>
</head>
<body>
<input id="myButton" type="button" value="Buy another product" />
<form method="post" action="addInput.php">
<div id="myOrderForm"> //input tags here
Product: <input type="text" name="myOrder[]" /><br>
Product: <input type="text" name="myOrder[]" /><br>
</div>
<input type="submit" value="Place Order">
</form>
</body>
</html>
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
$(文档).ready(函数(){
$(“#我的按钮”)。单击(函数(){
//如果需要,创建循环以添加十个输入
$(“#myOrderForm”).append('Product:
');
});
});
//在这里输入标签
产品:
产品:
php文件addInput.php
<?php
$myOrders=$_POST["myOrder"];
for ($i=0; $i<count($myOrders); $i++)
{
// validate each array member with if (empty($myOrders[$i]))
// do something
echo $myOrders[$i]."<br>"; // echo just for example
}
?>
“我应该如何创建表单名称?”-输入字段名称应该是相同的,并且在PHP端被视为一个数组。(也就是说,所有的零件号字段应该彼此具有相同的名称
,所有的数量字段应该彼此具有相同的名称
。)我不是PHP爱好者,但我认为如果你像Qty[]
那样命名它们,这对PHP有用吗?忽略空行的一种方法是在提交时使用jQuery循环行,然后删除它们或删除它们的name
属性,这样它们就不会被提交。但是,无论哪种方式,您都应该在PHP端验证数据。
<?php
$myOrders=$_POST["myOrder"];
for ($i=0; $i<count($myOrders); $i++)
{
// validate each array member with if (empty($myOrders[$i]))
// do something
echo $myOrders[$i]."<br>"; // echo just for example
}
?>