PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP HTTP & HTTPSPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Forms Sticky

Sticky inputs are auto-filling inputs after submitting

In previous examples, if user forgot to fill the email, an error message was shown. Also, all other inputs were cleared. Sticky inputs prevents this annoying mistake.

To do this, the form and handler should be on the same script. It is pretty easy to make our form sticky. We need to echo the submitted name and email as the value attribute of the input fields.

Sticky Input Fields



	$name = $email = '';

	if (empty($_POST['name'])) {
		$nameError = 'Name should be filled';
	} else {
		$name = trim(htmlspecialchars($_POST['name']));

	if (empty($_POST['email'])) {
		$emailError = 'Please add your email';
	} else {
		$email = trim(htmlspecialchars($_POST['name']));

	<title>PHP Forms</title>
	<style type="text/css">
		.error {

<form method="POST" action="">
	Name: <input type="text" name="name" value="<?php if (isset($name)) echo $name; ?>">
	<span class="error"><?php if (isset($nameError)) echo $nameError ?></span>

	Email: <input type="text" name="email" value="<?php if (isset($email)) echo $email; ?>">
	<span class="error"><?php if (isset($emailError)) echo $emailError ?></span>
	<input type="submit" name="submit">


Run Example ››
Visit PHP Help group to get some help from experts.
Profile Picture
Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.
My Websites