fortrustit
fortrustit

Reputation: 91

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

Upvotes: 1

Views: 192

Answers (2)

A H Bensiali
A H Bensiali

Reputation: 875

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

Upvotes: 1

Abu Yousef
Abu Yousef

Reputation: 570

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>

Upvotes: 2

Related Questions