PHP & Input Text box with default value and Retaining text box value

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.

Text box within a form

<form method=POST action=pb-text2.php>
<input type=text name=t1>
<input type=submit value=Submit ></form>

Receiving the textbox data

Form is submitting the data by attribute action=pb-text2.php to pb-text2.php file, inside which we will keep this code to receive data and display the same.
<?Php
$t1=$_POST['t1'];
echo $t1;
?>

Keeping Default data inside Textbox

We will use default attribute to keep some default data inside input textbox. Source of this data can be a variable or we can display by taking data from a database table. 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
<?Php
$t1="My Data here "
echo "<input type=text name='t1' value='$t1'>";
?>
Outpt is here

Redirection of submitted data to input form

In this text box we will store the value entered by the user. This form is submitting to another page where the PHP code will collec the data and 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.

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.
<?Php
$t1v=$_GET['t1v']; $t1v=urldecode($t1v); 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
<?Php
$t1=$_POST['t1'];

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

Demo of the textbox script

Example : Updating data from database

You are asked to update your address, the site will collect your present address from a database table and show it to you as default data inside a textbox. You can change the data and submit to update the table with your new address.

Name

Address 1

Address 2

City

State

Country

Zip

Visitors Rating
Your Rating





23232323

21-09-2018

vbvb

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




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer