我有一个表单,需要10行类似的数据。该表格收集产品代码、说明和数量。我循环浏览10行,并使用数组来收集信息。
$code = array();
$description = array();
$quantity = array();
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="<?php echo $code[$i]; ?>" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="<?php echo $description[$i]; ?>" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="<?php echo $quantity[$i]; ?>" />
</div>
</div>
<?php
}
?>
在下一页中,我使用$_POST['code'], $_POST['description'], $_POST['quantity']
将数据转发并尝试使用它
我的问题是数据似乎没有到达?
使用for循环,我还能提交表单并将所有数据转发吗?
希望这是尽可能多的信息,谢谢!
您在name属性中给出数组的值。你的数组是空的,所以你的名字也是空的。
试试这个:
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="code[]" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="description[]" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="quantity[]" />
</div>
</div>
<?php
}
?>
name[]格式会自动使数据成为数组。
与$_POST
数组一起使用的键是您放入name=""
属性中的任何键。根据您提供的代码,名称不是code
、description
和quantity
,而是物品的实际代码、说明和数量。你可能想这样做:
$code = array();
$description = array();
$quantity = array();
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="code[]" value="<?php echo $code[$i]; ?>" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="description[]" value="<?php echo $description[$i]; ?>" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="quantity[]" value="<?php echo $quantity[$i]; ?>" />
</div>
</div>
<?php
}
?>
有几个地方需要更新代码才能按预期工作。
最重要的是,输入使用了错误的属性来存储名称和值。
例如,对于您的每个输入,输入元素都需要看起来像这样:
<input type="text" class="quotecode" name="code[]" value="<?php echo $code[$i]; ?>" />
添加一个提交按钮和周围的表单标签后,您可以使用PHP$_POST或$_GET变量继续检查下一页中的变量。