How to Retrieve Data from a Text box with PHP 


               


How to Retrieve Data from a Text box with PHP



PHP


In this article, we will show how you can retrieve data from a text box with PHP.

Text boxes are used all over the web in various types of forms. They are used so that a visitor to a site can enter information into it for all various types of reasons, including signing up for a newsletter, joining to become a member of site, filling out credit card numbers to make a purchase, and so on.

As an example, let's say we're making an HTML form, such as the one shown below, which asks for a person's name:








The name you entered is


We know that users can enter information via text boxes. But how do we extract the data from the forms so that we can do whatever we want with it? How do we now take the information that this person entered and do something useful with it, such as create a confirmation page or put it into a database for storage so that we can keep a list of all the people entering into this form? Or any task?

And the answer is, we can do this using PHP.

Using PHP, we can extract the information that is input into a text box and either post it on a web page, such as create a confirmation page with the data that the user has entered or store it into a database. In this article, we focus on extracting the information and displaying it on a web page.

Coding

HTML Code

The HTML code to create the text box you see above, asking for a user's name is:



In this HTML code, we create a form in which we create a text box within the form asking for the visitor's name. To create a form on a web page, we must enclose whatever it is in the form with <form></form> tags. Within the form tags are attributes. The first attribute, the action attribute, is set equal to an empty string ("") or null. This is because this current page will process the PHP code. If we were sending the PHP code to another page, that PHP page would be specified within the action attribute. The other attribute is the method attribute. This means that the information will be posted to the page instead of appended to the current URL. For a more in-depth understanding of the method attribute, see Difference between the POST and GET Method in PHP.

The code which creates the text box is <input type="text" name="name_of_person"/>. This creates a text box and assigns it to the name attribute of name_of_person.

We then create a submit button.

PHP Code

The PHP code to extract the information which the user enters and display it is:



How we retrieve the information using PHP is shown above. To retrieve the information from the text box, we use its name attribute. The name attribute of the text box, in this case, is name_of_person. Therefore, to extract the information from this name_of_person text box, we use the line $name= $_POST['name_of_person'];. The $_POST is a superglobal array which extracts information from HTML form fields. In this case, it extracts the data from the text box with a name attribute of name_of_person and assigns and stores it to the $name variable.



Once we've retrieved the data which the user has entered using the superglobal $_POST array, we now use the echo statements to output the data which the user entered wherever you want it on the web page.

Therefore, since we are placing the PHP output below the text box, it goes below the text box after a few <br> tags.

And this is how information can be retrieved from a text box using PHP.

Note: In order for PHP to retrieve information from a text box, the PHP code to retrieve the data must come before the text box. It cannot come after. So in this case, since we have a text box, the PHP code to retrieve the data from the text box must come before the actual HTML text box.

Related Resources

How to Retrieve Data from a Radio Button with PHP

How to Retrieve Data from a Check box with PHP

How to Retrieve Data from an Array of Checkboxes with PHP

How to Retrieve Data from a Drop-down List with PHP

How to Retrieve Data from a List Box with PHP

How to Retrieve Data from a Textarea with PHP



HTML Comment Box is loading comments...