PHP Data Retention

 

In the previous sections, you've been following along and building up a HTML form. You've learned how to get the text from a text box on a form, but there is a problem.

When the basicForm.php form is submitted, the details that the user entered get erased. You're left with the VALUE that was set in the HTML. For us, username kept appearing in the text box when the button was clicked. You can keep the data the user entered quite easily.

Your script should now look like the one in the link below. If not copy and paste this script, and test it out on your server. (Save the script as basicForm.php.)

The basicForm.php script

If you look at the VALUE attribute of the text box in the HTML from the above script, you'll see that it's set to "username". Because the form gets posted back to itself, this value will keep re-appearing in the textbox when the page is submitted. Worse, if you've left the Value attributes empty then everything the user entered will disappear. This can be very annoying, if you're asking the user to try again. Better is to POST back the values that the user entered.

To post the details back to the form, and thus keep the data the user has already typed out, you can use this:

VALUE="<?PHP print $username ; ?>"

In other words, the VALUE attribute is now a PHP line of code. The line of code is just this:

<?PHP

print $username ;

?>

It's a bit hard to read, because it's all on one line.

You also need to amend your PHP code in the HEAD section to include an else statement:

if (isset($_POST['Submit1'])) {

$username = $_POST['username'];

if ($username == "letmein") {

print ("Welcome back, friend!");

}
else {

print ("You're not a member of this site");

}

}
else {

$username ="";

}

In the else statement at the end, we're just setting the value of the variable called $username for when the button is NOT clicked, i.e. when the page is refreshed.

However, there are some security issues associated with textboxes (and other form elements). So we'll see a more secure way to handle these in a later section.

But our new line of HTML for our textbox reads like this:

<INPUT TYPE = 'TEXT' Name ='username' VALUE="<?PHP print $username ; ?>">

In other words, we're now printing out the VALUE attribute with PHP code.

 

Now that you know a few things about getting values from HTML forms, here's a few exercise

Exercise
Add two text boxes and a Submit button to a HTML form. Invite the user to enter a first name and surname. When the button is clicked, print out the person's full name. Don't worry about what is in the text boxes after the button is clicked.

Exercise
Using the same form as the previous exercise, display the first name and surname in the textboxes, instead of printing them out.

Exercise
Suppose your web site has only 5 users. Create a HTML form to check if a visitor is one of the 5 users. Display a suitable message.

 

In the next section, we'll take a look at how to handle Radion Buttons on a HTML Form.