Php:提交echo td字段和ad new td

时间:2011-04-18 12:28:07

标签: php field

我有以下代码:

<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>

<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>

<?php for($i = 0; $i < $count; $i++): 
    // Loop through all rows gathering the data here, and then creating the fields below
    $val0 = isset($_POST['field'][$count]['0']) ? $_POST['field'][$count]['0'] : '';
    $val1 = isset($_POST['field'][$count]['1']) ? $_POST['field'][$count]['1'] : '';
    $val2 = isset($_POST['field'][$count]['2']) ? $_POST['field'][$count]['2'] : '';
?>
<tr>

    <td><input name="field[<?php echo $count; ?>][0]" value="<?php $val0; ?>"/></td>
    <td><input name="field[<?php echo $count; ?>][1]" value="<?php $val1; ?>"/></td>
    <td><input name="field[<?php echo $count; ?>][2]" value="<?php $val2; ?>"/></td>
</tr>
<?php endfor; ?>

</table>

<input type="submit" value="click me" />
</form>

问题是,当我按提交时,它会添加3个其他字段,但它会清除其他字段。如何保留字段中的内容但使其不可编辑?

2 个答案:

答案 0 :(得分:0)

for($j=0;$j<3;$j++){
  echo '<input type="hidden" name="field['.$i.'][0]" value="'.$_POST[field][$i][0].'" />';
}

答案 1 :(得分:0)

你没有回应你的价值观,你的 $ count 应该 $ i ,因为你最终会得到相同的字段名称

<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>

<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>

<?php for($i = 0; $i < $count; $i++): 
    // Loop through all rows gathering the data here, and then creating the fields below

    $val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
    $val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
    $val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>

    <td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
    <td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
    <td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>

</table>

<input type="submit" value="click me" />