Retaining text box value of a form after posting

Learn details on retaining post back values of a form here

Here is one text box inside a PHP code area. We will learn here how to retain the text box value after the form is submitted. In this text box we will keep one value option which will store the value entered by the user. This form is submitting to another page where the PHP code will check the value and if it is correct then it will redirect ( or back ) to main page with the value of data in query string. We will use PHP header redirect in the second page ( page to which form data will be submitted ) to post back the data to the main page having the form.

Assigning php variable to at text box

Before this we will learn how to assign data stored in a php variable to value attribute of a text box. When the page is loaded the data stored in PHP variable will be stored and displayed inside text box as default value. Here is one example

$t1="My Data here "
echo "<input type=text name='t1' value='$t1'>";

Here to take care of some characters like & we have used urlencode and urldecode to first encode the date before sending through the address bar and then after receiving at main page again we will decode it to get the original data entered by the visitor.

Here is the code of the form with decode script.


echo "
<form method=post action=pb-t.php>
<input type=text name=t1 value='$t1v'>
<input type=submit value=Submit>
"; ?>

Now we will go to the second page were the form date is collected and posted back to main page. Here is the code of pb-text.php


header ("Location: pb-text.php?t1v=$t1");

Demo of the textbox script

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer