2012-03-20 50 views
2

我有一个需要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循环,我仍然可以提交表单并将所有数据转发吗?

希望这是尽可能丰富,谢谢!

+0

'var_dump'你的$ _POST数组,看看有什么要发送 – 2012-03-20 13:00:24

回答

1

您在名称属性中给出数组的值。你的数组是空的,所以你的名字也是空的。

试试这个:

<?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 
} 
?> 

名】格式会自动让你的数据的数组。

+0

我有一种感觉,这是一件很基本的,已经有一段时间,因为我这样做,只是犯了一个错误包含名称和值属性的基础知识。谢谢你的回复! – sark9012 2012-03-20 13:11:38

1

有几个地方的代码需要更新才能按预期工作。

最重要的是输入使用错误的属性来存储名称和值。

例如,输入元素需要看起来像这样为您的每个输入:

<input type="text" class="quotecode" name="code[]" value="<?php echo $code[$i]; ?>" /> 

后,将提交按钮和周围的表单标签,你可以再继续检查在旁边的变量页面使用PHP $ _POST或$ _GET变量。

1

使用$_POST数组的关键是您在name=""属性中所做的任何操作。根据您提供的代码,名称不是code,descriptionquantity,但是无论实际的代码,说明和项目的数量是多少。你可能想这样做,而不是:

$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 
} 
?>