简体   繁体   English

HTML表单元素在提交后消失

[英]HTML Form elements disappearing after submitting

Basically I have 2 forms here, the first is a simple drop down select where you choose how many 'sections' you want, each of these 'sections' requires a key and a value, so when the form is submitted, it runs a loop and generates input boxes into another form for the amount selected. 基本上我在这里有两个表单,第一个是简单的下拉选择,你可以选择你想要多少'部分',每个'部分'需要一个键和一个值,所以当提交表单时,它运行一个循环并为所选金额生成另一种形式的输入框。 This all works fine. 一切正常。 When this second form is submitted, the input boxes disappear. 提交第二个表单后,输入框将消失。 It does post the data and the strings seems. 它确实发布数据和字符串。

      <?php 
    $userkey = $_POST['key1'];
    $userval = $_POST['val1'];
    $usernum = $_POST['usernum'];
    ?>
<form action='MYPIE.PHP' method='POST'>
                HOW MANY SECTIONS?
                    <select name="usernum">
                          <option>1</option>
                          <option>2</option>
                          <option>3</option>
                          <option>4</option>
                          <option>5</option>
                          <option>6</option>
                          <option>7</option>
                          <option>8</option>
                          <option>9</option>
                    </select>
                <input type="submit" name="submitnum" value="submit" />
        </form>


    <form action='MYPIE.PHP' method='POST'>     
        <?php 
            for ($i=1; $i<$usernum+1; $i++){
                echo "<br>insert key:   <input name='key".$i."' value='hi".$i."'>   insert value:   <input name='val".$i."' value='val".$i."'>";
            }
        ?>
        </br>
        <button type="submit" value="submit" name="submit keys" />
    </form>

This is because the post data isnt stored the second time you submit the form. 这是因为第二次提交表单时不会存储帖子数据。 You can fix this with a hidden input containing your data. 您可以使用包含数据的隐藏输入来解决此问题。

  <form action='MYPIE.PHP' method='POST'>     
    <?php 
        echo '<input type="hidden" value="' . $usernum . '" name="usernum" />'; 
        for ($i=1; $i<$usernum+1; $i++){
            echo "<br>insert key:   <input name='key".$i."' value='hi".$i."'>   insert value:   <input name='val".$i."' value='val".$i."'>";
        }
    ?>
    </br>
    <button type="submit" value="submit" name="submit keys" />
</form>

the line: <input type="hidden" value="' . $usernum . '" name="usernum" /> will resend the usernum data the second time you submit the form. 行: <input type="hidden" value="' . $usernum . '" name="usernum" />将在您第二次提交表单时重新发送usernum数据。

$usernum doesn't stay after you submit the second form. 提交第二个表单后, $usernum不会停留。 It's only in the first form. 它只是第一种形式。 To fix this, make a hidden form element in the second form. 要解决此问题,请在第二种形式中创建隐藏的表单元素。

<input type="hidden" name="usernum" value="<?php echo $usernum; ?>" />

This will cause that value to be submitted not only when the first form is submitted, but the second as well, and your loop will display the correct number of inputs. 这将导致不仅在提交第一个表单时提交该值,而且第二个表单也提交,并且您的循环将显示正确的输入数量。

that's because you lose the posted usernum , try; 那是因为你丢失了发布的usernum ,试试;

<?php 
     for ($i=1; $i<$usernum+1; $i++){
           echo "<br>insert key:   <input name='key".$i."' value='hi".$i."'>   insert value:   <input name='val".$i."' value='val".$i."'>";
        }
 ?>
<input type="hidden" name="usernum" value="<?php echo $usernum; ?>">

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

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