简体   繁体   中英

Put input value back to input filed

I have two submit buttons in one form and when I used the first submit, all the text input fields become blank. I try to solve this by using a session to store the textfield inputs.However, I don't know how I can put back the value to the input field that I stored in the session after the first submit is used.

The HTML code:

<form method="post" enctype="multipart/form-data"> 
    <input type="text" name="headline" id="headline" required />
    <input type="file" name="files[]" multiple>
    <input type="submit" value="Upload Images" name="submit_image"> 
    <input type="submit" name="submit_post" value="Submit Your Post"/>
</form>

The php code:

<?php
session_start();
if (isset($_POST["submit_image"])) {
    if (!empty($_POST['headline'])) {
       $_SESSION["headline_session"] = $_POST ["headline"]; 
    }
}
$headline_session = $_SESSION["headline_session"];
?>

Why you want to use a session! you can echo the value of $post parameter in a property called (value) like this

 <form method="post" enctype="multipart/form-data"> <input type="text" name="headline" id="headline" value="<?php echo $_POST ['headline']; ?>" required /> <input type="file" name="files[]" multiple> <input type="submit" value="Upload Images" name="submit_image"> <input type="submit" name="submit_post" value="Submit Your Post"/> </form> 

You can still save things to session if you need them later but I recommend just echoing out the previous value.

<form method="post" enctype="multipart/form-data">

    <input type="text" name="headline" id="headline" value="<?php echo $_POST['headline']; ?>" required />

<input type="file" name="files[]" multiple>
<input type="submit" value="Upload Images" name="submit_image"> 
<input type="submit" name="submit_post" value="Submit Your Post"/>
</form>

Should you need to compare the previous submissions with what was already submitted then this is one way to do it.

Good work thinking forward :) happy coding

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