Working with HTMLForms in PHP
Form Handling in PHP
Table of contents
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?
- Users can communicate with the server using HTML pages.
- These pages are transmitted over HTTP protocol on port 80.
- Forms generally opened it in the browser.
- Form contains various types of UI(UserInterface) controls
- forms created within the body tag of HTML pages.
- 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
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>
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.
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>
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.
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.
- PHP filename ----------------------hello.php
- arguments list and the -----------user=John
- button name user clicked -------submit=Go
? Separates server filename and the arguments list.
& separates each argument.
Comments
Post a Comment