Working with HTMLForms in PHP

Form Handling in PHP




Table of contents

  1. Web Application Architecture
  2. Example Form
  3. Connecting form with PHP file.


In Dynamic web applications  to communicate with the user, HTML forms are used. Forms collects the data and submits to PHP Page. To accept data from users, there are various types of form controls available in HTML. Example, text-fields, Password field, drop-downs, radio buttons, check boxes etc. with these we can create different types of forms ex: login forms, registration forms, and also contact forms feedback forms and so on. Forms are the fundamental interface between the user and the server. Below diagram explains the key role of the forms in web application.


Web Application Architecture

There are 3 layers in  Web application architecture 

 

 

  • User sends a request using the HTML page.
  • The server sends a response to the user by executing a PHP program.
  • Database if the user needs some information, the server(php program) reads it from the database and processes that data if needed, and sends the result to the user.

 So first, we focus on How to use HTML forms

Why forms?

  1. Users can communicate with the server using HTML pages.
  2. These pages are transmitted over HTTP protocol on port 80.
  3. Forms generally opened it in the browser.
  4. Form contains various types of UI(UserInterface) controls 
  5. forms created within the body tag of HTML pages. 
  6. One HTML page can have more than one form.

Example Form-Controls:

Below given image shows some of the most frequently used form controls. These are also called input elements or form fields etc., which accepts data from the user and submits to server page.

HTML Form Controls

coding-zon-html-forms-img

 
 
Create a folder in PHP server (XAMPP or WAMPP).

Create the following HTML file in it.

sample.html

  
<html>
 
<body>
 
Enter your Name: 
<input type="text" name="t1">
<input type="submit" name="submit" >
 
</body>
</html>


coding-zon-sample-code


Next and open it in browser, type some name in a textbox and submit it.

Then the form is not submitted because there is no form tag defined in it. To submit your request to the server, there should be a form inside HTML.

coding-zon-sample-form-img



Creating and Submitting form for server using PHP file.



Now we write another HTML file with form tag in it. 

Mostly, forms are created within the body tag. Each form will have one submit button which allow user to submit the request to server program(php file).

hello.html

<html>
 
<body>
 
<form name="form1" action="hello.php" method="get"> 
 
Enter your Name:<input type="text" name="user">
 
<input type="submit" name="submit" value="Go" >
 
</form> 
  
</body>
 
</html>

GoTop

coding-zon-hello-html-code
 

form tag contains 3 attributes:

name: name of the form
action: name of the PHP program that is linked with this form
method: method of transmitting data to the server
 

There are 2 method(built-in) variables available to use in form method attribute, these variables tells the way how data  transmit data to the server.

  • GET
  • POST


Open hello.html file in browser. Type name and click Go.

Move TOCcoding-zon-hello-form-img

Now Url Looks as follows;

http://localhost/coding-zon/phpforms/hello.php?user=John&submit=Go

When the form is submitted, the browser sends the following details to the server.

  1. PHP filename ----------------------hello.php
  2. arguments list and the -----------user=John
  3. button name user clicked -------submit=Go

   Separates server filename and the arguments list.

&  separates each argument.

 
Note: Browser say Object not found error. Because haven't yet defined the hello.php.

What you learned:

  • What is form
  • Why we use forms
  • Types of form Controls
  • Creating form
  • Submitting form

In the next blog you will learn, How to receive form data in PHP.




Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP