🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 85 (from laksa142)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
4 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://radu.link/keep-form-data-submit-refresh-php/
Last Crawled2026-04-10 07:46:12 (4 days ago)
First Indexed2021-07-26 17:42:47 (4 years ago)
HTTP Status Code200
Meta TitleHow to Keep Form Data After Submit and Refresh Using PHP - Radu.link
Meta DescriptionHere are two simple PHP methods to keep the form input data after the user submits the form and the page refreshes.
Meta Canonicalnull
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 ![](https://radu.link/images/r-avatar-1200.png) 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. ![](https://radu.link/images/r-avatar-1200.png) ### 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.
Shard85 (laksa)
Root Hash15007054701415423285
Unparsed URLlink,radu!/keep-form-data-submit-refresh-php/ s443