Press "Enter" to skip to content

How to Create Text Box and Display Its Value in PHP?

In this tutorial, you will learn how to create a text box and display its value in PHP. The text box we will create through HTML forms and will process through PHP post functions.

What is The HTML Form?

An HTML form allows a user to enter data and data can send to a server for processing. An HTML form contains form elements and Form elements are an input element, textarea element, label element, button element, select element, etc.

What is The Input Element?

Input element is defining by an <input> tag. These elements are using within a <form> element to specify input controls that help the user to input data.

What are The Attributes of an <input> Element?

Attributes are the modifiers of an HTML element type. An attribute can modify the default functionality of an element type. Attributes in HTML are type, name, value etc.

What is The Type Attribute on an <input> Element?

The type attribute is using to define the type of <input> element to display.

How to Create a Text Box?

Text box is a signle line text field. <input type=”text”> defines a text field.

What is Name Attribute of an <input> Element?

The name attribute defines the name of an <input> element. With this attribute, form elements can send their values to a server when we submit the form-data using a form.

What are The Attributes of <form> Element?

Action and Method.

What is Action Attribute of <form> Element?

Action attribute is using to send the form-data when we submit a form by clicking on a submit button. This attribute is specifying the URL where the form data are sending for processing a PHP file.

What is Method Attribute in <form> Element?

Method attribute determines how to send the form data to a server. <form> element contains two types of methods, one is the GET method, and another is POST method.

The default method of <form> element is GET. When we send data to a server through a GET method, then data appears on the URL with a limited data(maximum 2048 characters), and when we send the form-data to a server through a POST method, then form-data are invisible to the users with no limitation of sending information.

What is $_GET and $_POST in PHP?

$_POST is an array of variable names which is using to collect values from a form through a POST method and information are invisible to others.

$_GET is also an array of variables which is using to collect data from an HTML form using the GET method.

Now we are writing code for creating a text field, and a submit button where PHP codes are embedding in HTML.

There is a submit button which is helping to submit the data for processing the PHP script. Here after running the file, we are passing a value as “john” through a text field and we are clicking on the submit button to get the result as “The name of the person is:-john”.

<html>
    <body>
        <form action="" method="post">
            Name:
            <input type=text name="t1">
            <br>
            <br>
            <input type=submit name="s">
            <?php
if(isset($_POST['s']))
{
    $a=$_POST['t1']; //accessing value from the text field
    echo "The name of the person is:-".$a; //displaying result
}
            ?>
        </form>
    </body>
</html>

Output

PHP text box example.

In this file, we are using a text field with a name attribute and a submit button. We are using here a $_POST variable for accessing the value from the text field by clicking on the submit button. When we run file then we can input data for the text field and we can submit data for processing the PHP script by clicking on the submit button.

Now we are giving an another example for taking two inputs from two text fields and we are clicking on a submit button for processing the PHP script.

<html>
    <body>
        <form action="" method="post">
            First number:
            <input type=text name="t1">
            <br>
            Second number:
            <input type=text name="t2">
            <br>
            <br>
            <input type=submit name="s">
            <br>
            <?php
if(isset($_POST['s'])) // here isset function is using to check where a variable is set or not
{
    $a=$_POST['t1'];// accessing value from first text field 
    $b=$_POST['t2'];// accessing value from second text field
    $c=$a+$b; //addition of two values
    echo "The sum of two numbers is:-".$c;
}
            ?>
        </form>
    </body>
</html>

Output

PHP text field example to sum to numbers.

In this program we are accessing two values through two $_POST variables after clicking on the submit button and we are calculating the sum of two values with displaying the result.