Sticky Forms
8,915 ViewsPHP Tutorials March 7th, 2007
Learn how to create and use sticky forms, which saves data that has been entered into a form, so a user does not have to re-type out the information if there is an error.
In this tutorial I will take you from the basics of HTML and PHP, so you do not need any knowledge of either language to learn how to make stick forms!
The basic principal to sticky forms is that you are just taking the data sent by the form (Using a POST or GET method) and then just setting that data as the value for the input. If this explanation confuses you, read on as I explain it all in great detail!
I will be creating a form with just 2 inputs, your name and also a description of yourself. The name input will just be a text input and the description box will be a text area input.
So let's start off by creating our form.
-
<html>
-
<body>
-
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
Now, that above piece of code just starts a new HTML document with the <html> tag, then it opens the body using the <body> tag. Then we start off creating our form, the <form> tag tells the browser that you are starting a form, then the action=" " attribute tells the browser, where to go when the form is submitted. In this case I have used a little bit of PHP to make this script more dynamic. I have set it to <?php $_SERVER['PHP_SELF']; ?> what this will do is just put the name of the page you are on (Lets just say form.php) and will put it in there. So no matter what the name of your page is, it will change to meet your needs. Then the method attribute tells the browser whether or not to use the POST method or the GET method. The POST method is much more secure so I will be using that method for this
-
<p><b>Your Name : </b><input type="text" name="name" value="
-
<?php if(isset($_POST['name'])) echo $_POST['name'];
-
?>
-
" /></p>
The above piece of code is a very important piece of code, with lots of things to explain.
First off I use a <p> tag to start a new paragraph, then I put a <b> tag, anything within the <b> </b> tags is made bold. Then I start off a new input by using the <input> tag. The type=" " attribute is where you define what type of input you would like (Ex : text, checkbox, radio, etc..) For this tutorial we are using a text form input. Then the name=" " attribute is where we set the name of this specific input. Seeing as how this input is used to put your name in, that is what I will call it.
Next we have the most important piece of code. This is where the term "Stick Form" comes into play. As you can see I start a new PHP code block, then I start a new if statement.
What it is saying is, IF the input "name" is submitted THEN echo out the value of "name". The isset() function in PHP is used to find out if something has been submitted. Then we use a $_POST global variable to find out if the input of "name" has been submitted. If we were using GET as our form method (Ex : method="get") then we would have this set to $_GET['name'].
The above piece of code is pretty much the exact same as the one before it. This just tells the browser to create a textarea that is 35 columns wide and 5 rows tall. Then it uses the same "Stick Form" idea as it did in the previous input.
-
<p><input type="submit" name="submit" value="Submit" /></p>
-
</form>
-
</body>
-
</html>
This piece of code just creates a submit button to submit our form (And see sticky forms in action) and then we close off our form, body and html tags.
We should now have our finalized code which looks like this :
-
<html>
-
<body>
-
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
-
-
<p><b>Your Name : </b><input type="text" name="name" value="<?php if(isset($_POST['name'])) echo $_POST['name'];?>" /></p>
-
-
<p><b>Your Description : </b><br />
-
-
<p><input type="submit" name="submit" value="Submit" /></p>
-
</form>
-
</body>
-
</html>
Then we just need to save our file as something, I will be saving mine as form.php. Again though, you can save it as anything because the form is dynamic.
Now upload your file to your website and go to the file (form.php in my case). Enter in some info into the 2 inputs then click submit, you will see that the information that you entered into the text boxes stay there. You can use this method when you have error checking on your forms (When you check for empty forms). You just add this to your forms value=" " attribute so if a user gets an error on the form they don't have to re-type out everything that was correct once again.
Thank you for reading my tutorial,
If you have any questions or comments about this tutorial please feel free to use the contact form on this website to send us an e-mail!

(12 votes, average: 4.33 out of 5)










March 8th, 2007 at 7:06 pm
[...] Free Coding Tutorials Sticky Forms [...]
March 8th, 2007 at 11:14 pm
great tutorial, I use this on my site now as we speak!
March 17th, 2007 at 10:34 am
cool tut
April 2nd, 2007 at 4:18 am
When I use it,it works when you when I submit it to the same page as per example. But I can get it to work with validation if the page is redirected back when validation fails.
April 5th, 2007 at 11:32 am
Well, the problem with that is that when you re-direct to the same page your $_POST variable is empty. What you can do is just display the errors on top of the form on the same page.
You could also just display an error page where it says "Please press the back button to correct your errors".
April 5th, 2007 at 2:32 pm
Ein sehr hilfreiches Tutorial, vorallem gut für Anfänger, die mehr als ein "formmailer" basteln wollen.
thank you for sharing your knowledge
April 8th, 2007 at 7:30 am
This is beautiful! I am using it on a site I maintain for charity as we speak now and it has solved many problems.
To Bemi,
I had the same need to put the form variables back in the form if I needed to send them back. Rather than redirecting them I display an error page that tells them what fields they missed and displays a back button that is connected to a form filled with hidden fields populated by the $_POST data sent by the first form. For simplicity's sake I use the same field names in the hidden fields that exist on the original form.
They hit the button and any data they entered is filled in again already thanks to the code we just learned above.
Hope this helps.
April 11th, 2007 at 6:57 am
Hi,
It is very good for the beginners with the description which has been mentioned here.
August 20th, 2007 at 4:54 pm
i want to create a form that will display all the info to a new page. the problem is that when user needs to edit something on the data and press the back button, all the date in the fields are gone. is this possible using stick form?
November 23rd, 2007 at 12:03 am
Hi
Thanks for a great tutorial on this - I can make it work with an input field but how can I use it in a textarea? I don't want the user to have to retype their long message if capthca validation fails. Your sharing of wisdom is appreciated!
December 2nd, 2007 at 4:47 pm
How do you make drop downs sticky? I tried your method but it doesn't seem to work with option selected
June 23rd, 2008 at 2:32 am
In your finalized code block you have col="6" & rows="35". I think you wanted that inverted.
Tutorial was very good.
July 2nd, 2008 at 1:16 am
hi this is good toturial but i think it is for the bigners so it is best and easy. take care and enjoy it
July 2nd, 2008 at 1:18 am
??????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????
==========================================================================================================
----------------------------------------------------------------------------------------------------------
July 9th, 2008 at 10:53 am
Thanks for the torturing..It is so clear and easy to understand. great work, keep it on...the future is in your hand..