Introduction to PHP – Part9

advertisement

advertisement

Forms:

A form is an area where user can input data. The place in the form where user input data is called form element. A form element can be a text field, text area field, drop-down list, radio buttons and checkbox. In this tutorial we will apply the knowledge we have gained so far and use it with HTML forms. The best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. That means the data entered in HTML forms can be manipulated using PHP scripts.

Creating form

A form is created using <form> tag. To learn more about HTML please refer to HTML tutorials available in net. Let us start with a simple HTML form that we use for user feedback.

Example#1

<html>
<body>
<form > 
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

The output of the above HTML code will be

Collecting information from HTML form using POST method

As we already mentioned, the best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. After filling the form, when we pressed Submit Query button, everything we filled in the form will be available in PHP script.

Example#2

<html>
<body>
<form action="action.php" method="post">
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

We can notice that we added action and method in the above code, which tells the server what action is to be taken and what method is to be used. In this example, when we click on Submit Query button the page action.php is called and the method used is post. In order to access the form elements we should write action.php like this. Note that the file name should be action.php and saved in the same directory.

action.php

<?php
echo $_POST["name"]."<br/>";
echo $_POST["email"]."<br/>";
echo $_POST["subject"]."<br/>";
echo $_POST["message"];
?>

When we fill the form and click the submit button, action.php will be called, the URL will look like http://webanddesigners.com/action.php. Information filled in the form is now available in action.php page. post method use $_POST array to save the form information, the name of the form fields will be the key in the $_POST array.

Collecting information from HTML form using GET method

The following code is exactly same as code in example#2, except that the method used here is GET.

Example#3

<html>
<body>
<form action="action.php" method="get">
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

action.php will look like as the following code. Note that when GET method is used the data elements are available in $_GET array. The data elements are accessed in the similar fashion.

action.php

<?php
echo $_GET["name"]."<br/>";
echo $_GET["email"]."<br/>";
echo $_GET["subject"]."<br/>";
echo $_GET["message"];
?>

Comparison of GET and POST method

When we use GET method, the information sent is visible. It will display the variable and value in browser’s address bar. The address bar will look like http://webanddesigners.com/action.php?name=John&email=john@webanddesigner.com&subject=hi&message=This+is+the+test+message+using+get+method. There is a limitation in the amount of data send using GET method, maximum of 100 characters can be send. One best thing about GET method is that, the page can be bookmarked; this is because all the variables and data are available in URL.

When we use POST method the information sent is completely invisible. In addition, there is no restriction on the amount of information to be sent. Up to 8mb of data can be sent using POST method. However, because data and variables are not available in URL, it is not possible to bookmark the page. The URL will look like http://webanddesigners.com/action.php

 

 

 

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.