简体   繁体   English

从动态表单发布(多行)

[英]Posting from dynamic form (Multiple rows)

I have a form that creates form rows (fields) dynamically (add-delete) up to seven. 我有一个窗体,最多可动态创建(添加-删除)七个窗体行(字段)。 Each additional form row has an incrimental number appended to it (ie: name="product" next is name="product2".. "Product3" etc.). 每个附加的表格行都有一个递增的数字(即:name =“ product”接下来是name =“ product2” ..“ Product3”等)。 So I thought I would just use isset to see what is posted and only insert what is posted but my lack of skills has led to this not working. 所以我以为我只会使用isset来查看发布的内容,而只插入发布的内容,但是由于我缺乏技能,导致此功能无法正常工作。 See my code below: 请参阅下面的代码:

    mysql_select_db("inventory", $con);

    $sql="INSERT INTO shipped (id, type, client, product, color, quantity)
    VALUES ('$_POST[productid]','$_POST[type]','$_POST[client]','$_POST[product]','$_POST[color]','$_POST[quantity]')";
    if( isset($_POST['productid2']) ) {
    "INSERT INTO shipped (id, type, client, product, color, quantity)
    VALUES ('$_POST[productid2]','$_POST[type2]','$_POST[client2]','$_POST[product2]','$_POST[color2]','$_POST[quantity2]')";
    }

I planned to just add "isset" for each product but as it turns out this will only insert one of the set of post values and not both. 我计划只为每个产品添加“ isset”,但事实证明,这只会插入一组post值中的一个,而不是两个。 I am going to have up to product7 (so seven inserts at max). 我最多要有product7个(所以最多七个插入)。 Any help? 有什么帮助吗?

You would use the array in input fields 您将在输入字段中使用数组

<input type="text" name="productid[]" />
<input type="text" name="type[]" />
<input type="text" name="client[]" />
<input type="text" name="product[]" />
<input type="text" name="color[]" />
<input type="text" name="quantity[]" />

At server side: 在服务器端:

foreach($_POST['product'] as $k => $v){
   $sql="INSERT INTO shipped (id, type, client, product, color, quantity) VALUES 
       ($_POST['productid'][$k],$_POST['type'][$k],$_POST[client][$k]','$v','$_POST[color][$k]','$_POST[quantity][$k]')";

}

Ok so I did like the other answer as it seemed much more elegant than the solution I ended up using - but the fact is the other answer just did not work. 好的,所以我确实喜欢另一个答案,因为它似乎比我最终使用的解决方案优雅得多-但事实是另一个答案只是行不通。 So my solution just ended up just checking if the next form set of form fields is set, and if it is it displays the proper inserts. 因此,我的解决方案最终只是检查是否设置了表单字段的下一个表单集,以及是否显示了正确的插入内容。 See the first part here: 请参阅此处的第一部分:

mysql_select_db("inventory", $con);

if( !isset($_POST['productid2']) ) {
$sql="INSERT INTO shipped (id, type, client, product, color, quantity)
VALUES ('$_POST[productid]','$_POST[type]','$_POST[client]','$_POST[product]','$_POST[color]','$_POST[quantity]')";
}

if( isset($_POST['productid2']) ) {
$sql="INSERT INTO shipped (id, type, client, product, color, quantity)
VALUES ('$_POST[productid]','$_POST[type]','$_POST[client]','$_POST[product]','$_POST[color]','$_POST[quantity]'),
('$_POST[productid2]','$_POST[type2]','$_POST[client2]','$_POST[product2]','$_POST[color2]','$_POST[quantity2]')";
}

if( isset($_POST['productid3']) ) {
$sql="INSERT INTO shipped (id, type, client, product, color, quantity)
VALUES ('$_POST[productid]','$_POST[type]','$_POST[client]','$_POST[product]','$_POST[color]','$_POST[quantity]'),
('$_POST[productid2]','$_POST[type2]','$_POST[client2]','$_POST[product2]','$_POST[color2]','$_POST[quantity2]'),
('$_POST[productid3]','$_POST[type3]','$_POST[client3]','$_POST[product3]','$_POST[color3]','$_POST[quantity3]')";
}

Like this another four times... adding the other values each time. 像这样再四次...每次添加其他值。 This works for me because my form fields can only be added a max of seven times so it doesn't need to be infinite. 这对我有用,因为我的表单字段最多只能添加七次,因此不需要是无限的。 So if you are only allowing a small amount of form fields to be added this will work well. 因此,如果仅允许添加少量表单字段,则此方法会很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM