PHP and Text Boxes on HTML Forms

 

If you've been following along from the previous sections then your basicForm.php now has a METHOD and ACTION set. We're going to use these to process text that a user has entered into a text box. The METHOD attribute tells you how form data is being sent, and the ACTION attribute tells you where it is being sent.

To get at the text that a user entered into a text box, the text box needs a NAME attribute. You then tell PHP the NAME of the textbox you want to work with. Our text box hasn't got a NAME yet, so change your HTML to this:

<INPUT TYPE = "Text" VALUE ="username" NAME = "username">

The NAME of our textbox is username. It's this name that we will be using in a PHP script.

To return data from a HTML form element, you use the following strange syntax:

$_POST['formElement_name'];

You can assign this to a variable:

$Your_Variable = $_POST['formElement_name'];

Before we explain all the syntax, add the following PHP script to the HTML code you have so far. Make sure to add it the HEAD section of your HTML (the part to add is in bold):

<html>
<head>
<title>A BASIC HTML FORM</title>

<?PHP

$username = $_POST['username'];
print ($username);

?>

</head>

Save your work again, and click the submit button to run your script. (Don't worry if you see an error message about "Undefined index". Click the button anyway.) You should see this appear above your text box:

Get the Username from the text box

Delete the text "username" from the textbox, and click the button again. Your new text should appear above the textbox. The text box itself, however, will still have "username" in it. This is because the text box is getting reset when the data is returned to the browser. The Value attribute of the text box is what is being displayed.

So how does it work?

The $_POST[ ] is an inbuilt function you can use to get POST data from a form. If you had METHOD = "GET" on your form, then you'd used this instead:

$username = $_GET['username'];

So you begin with a dollar sign ($) and an underscore character ( _ ). Next comes the METHOD you want to use, POST or GET. You need to type a pair of square brackets next. In between the square brackets, you type the NAME of your HTML form element – username, in our case.

$_POST['username'];

Of course, you need the semi-colon to complete the line.

Whatever the VALUE was for your HTML element is what gets returned. You can then assign this to a variable:

$username = $_POST['username'];

So PHP will look for a HTML form element with the NAME username. It then looks at the VALUE attribute for this form element. It returns this value for you to use and manipulate.

At the moment, all we're doing is returning what the user entered and printing it to the page. But we can use a bit of Conditional Logic to test what is inside of the variable. As an example, change your PHP to this:

$username = $_POST['username'];

if ($username == "letmein") {

print ("Welcome back, friend!");

}
else {

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

}

We're now checking to see if the user entered the text "letmein". If so, the username is correct; if not, print another message.

Try it out an see what happens. When you first load the page, before you even click the button, you might see the text "You're not a member of this site" displayed above the textbox. That's because we haven't checked to see if the Submit button on the form was clicked.

 

In the next part, we'll see how to check if the Submit button was clicked.