PHP Global Variables and HTML Form

PHP & MySQL Course: PHP Global Variables and HTML Form

In the previous lecture 11, we learnt about the advanced programming in PHP. One thing I should clear in this series of the course the advanced programming in PHP is not over yet. But in this lecture we will clear few topics which are most important to know about them.

Lecture 12:PHP Global Variables and HTML Form

In this lecture we will learn about the transferring of the data and their issues during the transfer of data. We will use three variables in PHP programming to transfer data from one webpage to another webpage. These two variables commonly called predefined variables as superglobal variables. And also we will use the HTML form tags to modify data. In this lecture will simply design a contact us webpage which is an important part for the commercial websites.

  1. HTML Form Tag
  2. $_POST variable
  3. $_GET variable
  4. $_REQUEST variable

So let’s we first of all define them what are they actually?

HTML Form Tag:

The HTML Form Tag is use to design a website with textboxes, comment boxes, buttons etc. we use <form> tag to start it and </form> to end the tag. And between them we use tags for textboxes, comment boxes etc. For detail lets we use it, check listing 1

Listing 1: HTML Form Tag


<html>

<head>

<title>Contact Us</title>

</head>

<body>

<h3>Contact-Us</h3>

<form>

<selection>

<option value="0"></option>

<option value="1">Mr.</option>

<option value="2">Miss.</option>

<option value="3">Mrs.</option>

</selection>

First Name: <input type="text" name="fname" /> <br>

Last Name:  <input type="text" name="lname" /> <br>

Email:              <input type="text" name="email" /> <br>

Message: <br><textarea type="text" name="message" col="100" rows="5" ></textarea> <br>

<input type="submit" vlaue="Submit" name="submit" />

</form>

</body>

</html>

Php Global variables

Figure 1  Form tag result as a contact us webpage

In the figure 1 you can see the output of the Listing 1. As I told you that using the form tag we can add buttons, text areas etc. The Listing 1 is just showing the pure HTML use of form, but we can design it in HTML 5.

HTTP Protocol:

Before we start to learn the predefined variable as they are superglobal variable too. But we should know about the HTTP. The HTTP is used to enable the communication between two things which are server and client. In the website the our application which is web browser is a client and we use the today’s lecture predefined variable to send communicate with server by sending data.

When we press the submit button the $_POST, $_GET, and $_REQUEST will send data to the server with help of HTTP. And then server will show the data as its reply.

$_POST:

$_POST actually is a variable of PHP, which is used for transferring data from one webpage to another webpages.  $_POST is a predefined variable and it is used to access data from forms when the submit button is pressed, then HTTP transfer the data to another webpage. And it is secure to transfer data from one webpage to another webpage. Lets we see how we can use the $_POST to submit data from one webpage to another webpage. Check the listing 2 for details

Listing 2: Using $_POST in PHP


<?

$FirstName = $_POST['fname'];

$LastName = $_POST['lname'];

$Email = $_POST['email'];

$Message = $_POST['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

$_GET:

This variable $_GET is same like $_POST but there are some difference between them. As we can bookmark the data from $_GET but we can’t do it from $_POST. The most important thing is that your data will be visible to everyone by using the $_GET but don’t if you use $_POST. $_GET is also an  superglobal variable which is commonly called predefined variable in PHP programming. Lets we see how we can use $_GET check the listing 3 for details.

Listing 3: Using $_GET in PHP


<?

$FirstName = $_GET['fname'];

$LastName = $_GET['lname'];

$Email = $_GET['email'];

$Message = $_GET['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

We just simple changed the program of Listing 2 in Listing 3’s program by replacing $_POST with $_GET, as you can see the difference between listing 2 and listing 3.

$_REQUEST:

If you don’t want to use the above two predefined variables so you can use the $_REQUEST place of them. There is a simple use of $_REQUEST built-in function, it is not a problem if we are using one of them from $_POST or $_GET in the form. $_REQUEST will perform both functionality, lets we define it in a PHP program. Just by replacing the $_POST and $_GET as we did in previous listings 2 from 1.

Listing 4: Using $_REQUEST in PHP


<?

$FirstName = $_REQUEST['fname'];

$LastName = $_REQUEST['lname'];

$Email = $_REQUEST['email'];

$Message = $_REQUEST['message'];

echo "<b>Full Name is: </b>".$FirstName." ".$LastName."<br>";

echo "<b>Email Address is: </b>".$Email."<br>";

echo "<b>Message is:</b> ".$Message."<br>";

?>

But one we didn’t do yet, that is to modify the form which we designed at the beginning of this lecture in the listing 1. For details check listing 5. In which we will use post keyword to send the request to the HTTP then the request will go to the server. Let’s see it

Listing 5: HTML Form Tag with Method POST


<html>

<head>

<title>Contact Us</title>

</head>

<body>

<h3>Contact-Us</h3>

<form action="contact-us.php" method="post">

<selection>

<option value="0"></option>

<option value="1">Mr.</option>

<option value="2">Miss.</option>

<option value="3">Mrs.</option>

</selection>

First Name: <input type="text" name="fname" /> <br>

Last Name:  <input type="text" name="lname" /> <br>

Email:                    <input type="text" name="email" /> <br>

Message: <br><textarea type="text" name="message" ></textarea> <br>

<input type="submit" vlaue="Submit" name="submit" />

</form>

</body>

</html>

As you can see the I have add a few things inside of the form tag that is method and action. If you are using the $_GET function in your PHP programming so you should just change the post in get from the method, but if you are using the $_REQUEST so you should not change anything from the form webpage.

Summary:

In this lecture we learnt the difference and uses of the three built-in functions as they are being used as a predefined variables and these are also called super global variables in PHP programming. These three variables works on HTTP protocol to create a communication between the client and server.

And also we used the form tag to design a contact form webpage. And inside the form tag in the action we wrote the name of the webpage file to whom we want to transfer the data after the authentication of the HTTP.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development