Working with HTML

Abhigyan Singh 01st Oct 2020

In this tutorial, we will discuss how HTML can be used with PHP.  The flexibility with PHP is that you can integrate HTML code with PHP and on the other hand you can integrate PHP with HTML code as well.

Here we will see both the examples. Lets first look at how can we integrate HTML code with PHP.

For Ex.

<?php
    echo "<h1>Hello DiscussDesk</h1>";
?>

Output

Hello DiscussDesk

As you can see in the above code you can write any HTML tag between pair of “ (double quotes) in the echo statement. This way you can integrate HTML code with PHP.

Now let’s take one example where we will integrate PHP code inside HTML code.

For Ex.

<h1>
<?php echo "Hello DiscussDesk"; ?>
</h1>

Output

Hello DiscussDesk

As you can see from the example you can write PHP code between any of the HTML tags, using <?PHP start and ?> end tag. This gives us more flexibility while writing PHP code along with HTML.

HTML Forms

Generally, a form is a special container where all user controls are placed.  Forms are the means by which a PHP page gets its input from users.

Syntax

<form action=”filename.php” method=”get/post”>

Where the action contains the name of the file which is to be to redirect along with the inputs feed by the user when the form is submitted. The method parameter is the other argument which defines how the input is to be transferred to the auction page.

Form Submission methods

There are two methods HTML forms can be submitted.

  1. GET
  2. POST

GET

When a form is submitted using GET Method it passes all its arguments (inputs from user) on the URL.  The URL is appended with the arguments (inputs from the user). The arguments and page URL is separated by? (Question mark).

For Ex.

<form action="Next.php"  method="get">
<input type="text" name="uname">
<input type="submit" name="submit" value="ClickMe">
</form>

When the form is submitted by clicking on the button its redirected to page Next.PHP along with the inputs given by the user.  If you have observed the URL it will look like

https://localhost/TEST/Next.php?uname=DiscussDesk&submit=ClickMe

as you can see https://localhost/TEST/Next.php is the URL of the action page, followed by? (Question Mark) which is followed by the set of inputs given by the user each input is divided into two parts name of control and the value entered. Multiple inputs are separated by &.

Here are some important points to remember while using the GET Method

  • GET method passes all the information appended on the URL.
  • The length of characters to be sent is restricted to 1024 characters so you can not send characters more than that.
  • Never use the GET method if any sensitive or secure data needs to be passed between pages. (i.e.  passwords)
  • GET method does not allow to send binary data like images, documents
  • The $_GET associative array is used to access the passed information on the action page.

Let's look at how to access the information on the Action page.

Next.PHP

<?php
    print_r($_GET);
?>

Here as you can see on the action page $_GET associative array is used for accessing the information. The index of elements will be the name you gave to the input element.

Output

Array ( [uname] => DiscussDesk [submit] => ClickMe )

POST

When a form is submitted using the POST method it passes all its arguments in a secure way through HTTP headers, in short, the arguments will not be visible on the URL, so whenever you want to pass more secure information POST is the go to choice.

For Ex.

<form action="Next.php"  method="post”>
<input type=”text” name=”uname”>
<input type=”submit” name=”submit” value=”ClickMe”>
</form>

When the form is submitted by clicking on the button it’s redirected to page Next.PHP along with the inputs given by the user.  If you have observed the URL it will look like

https://localhost/TEST/Next.php

so there will be no arguments passed on URL which was there in the case of getting Method.

Here are some important points to remember while using the POST Method

  • The information will be passed on the action page through HTTP headers so it will not be visible on the URL.
  • POST method is used when you want to pass secure information like password
  • The POST method has no restriction on the number of characters to be sent.
  • POST method allows sending binary data like images, documents.

Let us look at how to access the information on the Action page.

Next.PHP

<?php
    print_r($_POST);
?>

Here as you can see on the action page $_POST associative array is used for accessing the information. The index of elements will be the name you gave to the input element.

Output

Array ( [uname] => DiscussDesk [submit] => ClickMe )

To conclude in this tutorial we have learned how to use HTML forms and its types,  we will look at HTML form elements in the next tutorial.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk