简体   繁体   中英

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.

$usernum doesn't stay after you submit the second form. 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;

<?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; ?>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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