Working with HTML

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 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 tag , using <?php start and ?> end tag. This gives us more flexibility while writing PHP code along with HTML.

HTML Forms

Generally a form is the 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 file which is to be to redirected along with the inputs feed by user when the form is submitted. The method parameter is the other argument which defines how the input is to be transferred to the action 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 URL.  The URL is appended with the arguments (inputs from 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 user.  If you have observed the URL it will look like

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

as you can see http://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 user each input is divided in two parts name of control and the value entered. Multiple inputs are separated by &.

Here are some important points to remember while using GET Method

  • GET method passes all the information appended on URL.
  • The length of characters to be sent is restricted to 1024 characters so you can not send characters more than that.
  • Never use 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 action page.

Lets look at how to access the information on Action page.

Next.PHP

<?php
    print_r($_GET);
?>

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

Output

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

POST

When a form is submitted using POST method it passes all its argument in secure way through HTTP headers, in short the arguments will not be visible on 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 user.  If you have observed the URL it will look like

http://localhost/TEST/Next.php

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

Here are some important points to remember while using POST Method

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

Lets look at how to access the information on Action page.

Next.PHP

<?php
    print_r($_POST);
?>

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

Output

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

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

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments