âšď¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://radu.link/keep-form-data-submit-refresh-php/ |
| Last Crawled | 2026-04-10 07:46:12 (4 days ago) |
| First Indexed | 2021-07-26 17:42:47 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Keep Form Data After Submit and Refresh Using PHP - Radu.link |
| Meta Description | Here are two simple PHP methods to keep the form input data after the user submits the form and the page refreshes. |
| Meta Canonical | null |
| Boilerpipe Text | Whenever a user adds some invalid data to a form and submits it, the page will have to refresh to show the errors, in case you donât use JavaScript or something like that.
The bad news is that the input data will disappear, which can be very annoying, especially if the form has many fields.
Luckily, there are two simple methods that you can use.
1. Keep Form Data on Refresh Using the PHP Ternary Operator
The
ternary operator
is a shorter and more practical version of the standard if/else statement.
Itâs used like this:
<?
php
$a
?
$b
:
$c
;
This translates into something like this:
<?
php
if
(
$a
)
{
// do $b
$b
;
}
else
{
// do $c
$c
;
}
So, to use this in a form to remember the data after the user presses submit and the page refreshes, youâll have to add this in the
value=""
attribute of the input:
<?
php
echo
isset
(
$_POST
[
'name'
])
?
htmlspecialchars
(
$_POST
[
'name'
],
ENT_QUOTES
)
:
''
;
?>
You should wrap the output in
htmlspecialchars()
for security.
So, the input HTML tag should look like this:
<
input
name
=
"name"
type
=
"text"
value
=
"<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>"
>
What this does is to display the data in the
name
field
if
itâs set (added by the user). Otherwise, leave it empty.
2. Keep the Form Data on Refresh with the PHP Null Coalescing Operator
In PHP 7, the
null coalescing operator
was introduced, which is equivalent to the ternary operator, but shorter.
This:
<?
php
echo
$_POST
[
'name'
]
??
''
;
?>
is equivalent to this:
<?
php
echo
isset
(
$_POST
[
'name'
])
?
$_POST
[
'name'
]
:
''
;
?>
So, to keep the input data on submit and refresh, use it in the
value=""
attribute, like this:
<
input
type
=
"text"
name
=
"name"
value
=
"<?php echo $_POST['name'] ?? ''; ?>"
>
But, again, you should use
htmlspecialchars()
for security.
<
input
type
=
"text"
name
=
"name"
value
=
"<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>"
>
Note that you have to add the operator inside the
htmlspecialchars()
function. Otherwise, youâll get an âUndefined indexâ error.
You Canât Always Make Use of the HTML Value Attribute
There are cases where you canât use the
value=""
attribute to add a ternary or coalescing operator. So, you need to get more creative.
Letâs take the
<select>
HTML tag as an example, where youâll have to use the values for
$_POST
.
To keep the data on refresh in a
<select>
tag with PHP, you can make use of the
selected
HTML attribute, which makes an
<option>
to be selected by default.
So, youâll have to add something like this in the
<option>
tag:
<?
php
echo
(
isset
(
$_POST
[
'select'
])
&&
$_POST
[
'select'
]
===
'option1'
)
?
'selected'
:
''
;
?>
If
<select>
has a value, and if itâs equal to the
<option>
value, then echo
selected
, otherwise, donât echo anything.
Hereâs the whole part :
<
select
name
=
"select"
id
=
""
>
<
option
value
=
""
>
Select
</
option
>
<
option
value
=
"option1"
<?
php
echo
(
isset
($
_POST
['
select
'])
&&
$
_POST
['
select
']
===
'
option1
')
?
'
selected
'
:
'';
?
>
>Option 1
</
option
>
<
option
value
=
"option2"
<?
php
echo
(
isset
($
_POST
['
select
'])
&&
$
_POST
['
select
']
===
'
option2
')
?
'
selected
'
:
'';
?
>
>Option 2
</
option
>
</
select
>
Careful with
<textarea>
.
Unlike inputs,
<textarea>
has an ending tag:
</textarea>
.
So, you need to put the PHP operator between the tags, not inside.
<
textarea
name
=
"message"
>
<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>
</
textarea
>
I made this mistake, of course. đ
This Wonât Work for The File Input
The above methods wonât work for the File input, for security reasons.
And Iâm not aware of any other workaround with PHP. Perhaps with some Javascript.
So, the users will have to upload the file again if the page refreshes but the form isnât submitted successfully.
Thatâs a Wrap
I hope this post has helped you out.
If you're interested in my
web design services
or
free templates
, want to collaborate, or just have something to say, feel free to shoot me a message. |
| Markdown | - [Home](https://radu.link/)
- [Contact](https://radu.link/#contact)
- [Services](https://radu.link/#services)
- [Pricing](https://radu.link/#pricing)
- [Projects](https://radu.link/projects/)
- [Blog](https://radu.link/blog/)
[contact@radu.link](mailto:contact@radu.link)
[\#PHP](https://radu.link/tags/php/)
# How to Keep Form Data After Submit and Refresh Using PHP
December 9, 2019
â˘
Modified April 29, 2022

Available for new projects
Author: Radu
Impress your clients with [modern, stunning, fully customizable website templates](https://framer.link/k7ud9gc) ⢠Ad
Whenever a user adds some invalid data to a form and submits it, the page will have to refresh to show the errors, in case you donât use JavaScript or something like that.
The bad news is that the input data will disappear, which can be very annoying, especially if the form has many fields.
Luckily, there are two simple methods that you can use.
This only works on submit, either successful or unsuccessful (if validation errors trigger). It doesnât work on a simple page refresh (F5) if the data hasnât been submitted first. Youâll need some JavaScript for that.
## 1\. Keep Form Data on Refresh Using the PHP Ternary Operator
The [ternary operator](https://www.php.net/manual/en/language.operators.comparison.php) is a shorter and more practical version of the standard if/else statement.
Itâs used like this:
```
<?php $a ? $b : $c;
```
This translates into something like this:
```
<?php
if ($a) {
// do $b
$b;
} else {
// do $c
$c;
}
```
So, to use this in a form to remember the data after the user presses submit and the page refreshes, youâll have to add this in the `value=""` attribute of the input:
```
<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>
```
You should wrap the output in [htmlspecialchars()](https://www.php.net/manual/en/function.htmlspecialchars.php) for security.
So, the input HTML tag should look like this:
```
<input name="name" type="text" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>">
```
What this does is to display the data in the `name` field **if** itâs set (added by the user). Otherwise, leave it empty.
## 2\. Keep the Form Data on Refresh with the PHP Null Coalescing Operator
In PHP 7, the [null coalescing operator](https://www.php.net/manual/en/migration70.new-features.php) was introduced, which is equivalent to the ternary operator, but shorter.
This:
```
<?php echo $_POST['name'] ?? ''; ?>
```
is equivalent to this:
```
<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>
```
So, to keep the input data on submit and refresh, use it in the `value=""` attribute, like this:
```
<input type="text" name="name" value="<?php echo $_POST['name'] ?? ''; ?>">
```
But, again, you should use `htmlspecialchars()` for security.
```
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
```
Note that you have to add the operator inside the `htmlspecialchars()` function. Otherwise, youâll get an âUndefined indexâ error.
## You Canât Always Make Use of the HTML Value Attribute
There are cases where you canât use the `value=""` attribute to add a ternary or coalescing operator. So, you need to get more creative.
Letâs take the `<select>` HTML tag as an example, where youâll have to use the values for `$_POST`.
To keep the data on refresh in a `<select>` tag with PHP, you can make use of the `selected` HTML attribute, which makes an `<option>` to be selected by default.
So, youâll have to add something like this in the `<option>` tag:
```
<?php echo (isset($_POST['select']) && $_POST['select'] === 'option1') ? 'selected' : ''; ?>
```
If `<select>` has a value, and if itâs equal to the `<option>` value, then echo `selected`, otherwise, donât echo anything.
Hereâs the whole part :
```
<select name="select" id="">
<option value="">Select</option>
<option value="option1" <?php echo (isset($_POST['select']) && $_POST['select'] === 'option1') ? 'selected' : ''; ?>>Option 1</option>
<option value="option2" <?php echo (isset($_POST['select']) && $_POST['select'] === 'option2') ? 'selected' : ''; ?>>Option 2</option>
</select>
```
**Careful with** `<textarea>`.
Unlike inputs, `<textarea>` has an ending tag: `</textarea>`.
So, you need to put the PHP operator between the tags, not inside.
```
<textarea name="message">
<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>
</textarea>
```
I made this mistake, of course. đ
## This Wonât Work for The File Input
The above methods wonât work for the File input, for security reasons.
And Iâm not aware of any other workaround with PHP. Perhaps with some Javascript.
So, the users will have to upload the file again if the page refreshes but the form isnât submitted successfully.
## Thatâs a Wrap
I hope this post has helped you out.
If you're interested in my [web design services](https://radu.link/) or [free templates](https://radu.lemonsqueezy.com/), want to collaborate, or just have something to say, feel free to shoot me a message.

### About Radu
I've been working online from home for over 12 years, gaining valuable experience and developing a wide range of skills, including web design and development, optimization, and SEO.
[See All Posts](https://radu.link/posts/)
### Let's Connect
Reach out if you're interested in my design services, want to collaborate, or just have something to ask or say.
#### Links
- [Hire me on Fiverr](https://www.fiverr.com/wpmeisters)
- [Framer Profile](https://framer.link/radu-profile)
- [Free Framer Templates](https://radu.lemonsqueezy.com/)
#### Policies
- [Privacy](https://radu.link/privacy-policy/)
- [Terms](https://radu.link/terms/)
- [Refund](https://radu.link/refund-policy/)
Copyright Š 2018 - 2022 Radu.link
Created with Hugo and Tailwind CSS |
| Readable Markdown | Whenever a user adds some invalid data to a form and submits it, the page will have to refresh to show the errors, in case you donât use JavaScript or something like that.
The bad news is that the input data will disappear, which can be very annoying, especially if the form has many fields.
Luckily, there are two simple methods that you can use.
## 1\. Keep Form Data on Refresh Using the PHP Ternary Operator
The [ternary operator](https://www.php.net/manual/en/language.operators.comparison.php) is a shorter and more practical version of the standard if/else statement.
Itâs used like this:
```
<?php $a ? $b : $c;
```
This translates into something like this:
```
<?php
if ($a) {
// do $b
$b;
} else {
// do $c
$c;
}
```
So, to use this in a form to remember the data after the user presses submit and the page refreshes, youâll have to add this in the `value=""` attribute of the input:
```
<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>
```
You should wrap the output in [htmlspecialchars()](https://www.php.net/manual/en/function.htmlspecialchars.php) for security.
So, the input HTML tag should look like this:
```
<input name="name" type="text" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>">
```
What this does is to display the data in the `name` field **if** itâs set (added by the user). Otherwise, leave it empty.
## 2\. Keep the Form Data on Refresh with the PHP Null Coalescing Operator
In PHP 7, the [null coalescing operator](https://www.php.net/manual/en/migration70.new-features.php) was introduced, which is equivalent to the ternary operator, but shorter.
This:
```
<?php echo $_POST['name'] ?? ''; ?>
```
is equivalent to this:
```
<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>
```
So, to keep the input data on submit and refresh, use it in the `value=""` attribute, like this:
```
<input type="text" name="name" value="<?php echo $_POST['name'] ?? ''; ?>">
```
But, again, you should use `htmlspecialchars()` for security.
```
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
```
Note that you have to add the operator inside the `htmlspecialchars()` function. Otherwise, youâll get an âUndefined indexâ error.
## You Canât Always Make Use of the HTML Value Attribute
There are cases where you canât use the `value=""` attribute to add a ternary or coalescing operator. So, you need to get more creative.
Letâs take the `<select>` HTML tag as an example, where youâll have to use the values for `$_POST`.
To keep the data on refresh in a `<select>` tag with PHP, you can make use of the `selected` HTML attribute, which makes an `<option>` to be selected by default.
So, youâll have to add something like this in the `<option>` tag:
```
<?php echo (isset($_POST['select']) && $_POST['select'] === 'option1') ? 'selected' : ''; ?>
```
If `<select>` has a value, and if itâs equal to the `<option>` value, then echo `selected`, otherwise, donât echo anything.
Hereâs the whole part :
```
<select name="select" id="">
<option value="">Select</option>
<option value="option1" <?php echo (isset($_POST['select']) && $_POST['select'] === 'option1') ? 'selected' : ''; ?>>Option 1</option>
<option value="option2" <?php echo (isset($_POST['select']) && $_POST['select'] === 'option2') ? 'selected' : ''; ?>>Option 2</option>
</select>
```
**Careful with** `<textarea>`.
Unlike inputs, `<textarea>` has an ending tag: `</textarea>`.
So, you need to put the PHP operator between the tags, not inside.
```
<textarea name="message">
<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>
</textarea>
```
I made this mistake, of course. đ
## This Wonât Work for The File Input
The above methods wonât work for the File input, for security reasons.
And Iâm not aware of any other workaround with PHP. Perhaps with some Javascript.
So, the users will have to upload the file again if the page refreshes but the form isnât submitted successfully.
## Thatâs a Wrap
I hope this post has helped you out.
If you're interested in my [web design services](https://radu.link/) or [free templates](https://radu.lemonsqueezy.com/), want to collaborate, or just have something to say, feel free to shoot me a message. |
| Shard | 85 (laksa) |
| Root Hash | 15007054701415423285 |
| Unparsed URL | link,radu!/keep-form-data-submit-refresh-php/ s443 |