John23
John23

Reputation: 219

save fields values during form submit

I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.

Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.

Somebody there see something wrong?

<?php
if (isset($_POST['sendForm'])) {
    $f['name']    = $_POST['name'];
    $f['content'] = $_POST['content'];
    $f['date']    = $_POST['date'];
} ?>

<form name="form" action="" method="post">

    <label class="line">
        <span class="data">Name:</span>
        <input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
    </label>

    <label class="line">
        <span class="data">Content:</span>
        <textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
    </label>

    <label class="line">
        <span class="data">Date:</span>
        <input type="text" name="date" value="<?php if (isset($f['date'])) {
            echo $f['date'];
        } else {
            echo date('d/m/Y H:i:s');
        } ?>"/>
    </label>

    <input type="submit" value="Create" name="sendForm" class="btn"/>

</form>

Upvotes: 0

Views: 1716

Answers (1)

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

In short you can set by this way,

<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />

You do not need to use $f['name']. you can get value directly by $_POST['name'] method.

Upvotes: 1

Related Questions