简体   繁体   中英

How to keep values in re-opened form?

I have two PHP files: index.php with a form, and data.php for further data manipulation.

Here is index.php :

    <?php
        session_start();
        require_once("../index.conf");
        $language = new Language();
        $lang = $language->getLanguage(@$_POST['lang']);
    ?>
    ...
    <form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
    <input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
    ...
    <button class="btn_r" name="submit" type="submit">
        <?php echo $lang['submit-button']; ?>
    </button>

Here is data.php :

    // success message
        echo sprintf('
            <div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
                <form name="goto_preview" method="post">
                    <input type="hidden" name="img_title" value="'.$title.'">   
                    <button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
                    <button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
                </form>
            </div>',$img_name);

I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?

Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)

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