【问题标题】:Php: on submit echo td fields and ad new tdPhp:在提交 echo td 字段和 ad new td
【发布时间】:2011-08-07 20:30:09
【问题描述】:

我有以下代码:

<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 个其他字段,但它清除了其他字段。如何保留字段中的内容但使其不可编辑?

【问题讨论】:

    标签: php field


    【解决方案1】:
    for($j=0;$j<3;$j++){
      echo '<input type="hidden" name="field['.$i.'][0]" value="'.$_POST[field][$i][0].'" />';
    }
    

    【讨论】:

    • 谢谢,但在清除所有内容之前,它不会回显该字段中的内容
    • 在上面试试。你应该清理你的语法。
    【解决方案2】:

    您没有重复您的价值观,您的 $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" />
    

    【讨论】:

    • 如何使用下拉菜单而不是字段?
    • @user688013 下拉是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2011-11-21
    • 2012-04-17
    • 2013-12-20
    • 2018-04-23
    相关资源
    最近更新 更多