24 Aug 2015

PHP FORM

PHP Form Example

PHP form is used to take input from users. in PHP if you want to take input from keyboard and display the output according to input, in that case you have to use html form.
html form’s have a property : form method in which you have to set either get or post method.
To illustrate, consider the following  Web form(choose.php)
which asks you to select a brand of automobile and enter your desired color.

HTML Form

Save it as Choose.php

PHP Script

Output
Your blue Porshe 911 is ready. safe driving!
Select your car
Color
In the given above example:
First create a static page using HTML . Form first part is a drop down box from where user has to select the option, 2nd part is a text box in which user enter the color , after entered the value user click on button to display the output.

GET Method

Form GET Method

GET method is unsecured method because it display all information on address bar/ url.
By default method is get method. Using GET method limited data sends. GET method is faster way to send data.

In the given example user has to enter his/her name in text box, after entering the input he/she has to click on submit button to display the name entered by him/her.
You can see the inputted value in address bar(url) also.

Create HTML Form Where user enter their name

 

In the given above example:
user entered the name inside the text box , after entering the name he clicked on submit button and can see the output of the program means their name. User can check the input given by the user shows inside the URL because of get method.

Enter two number and print the sum of given numbers.

 

In the given above example:
user has to enter the first number, second number
after given the input click on “+” button, and check the output means the sum of two numbers.
can also see the input given by him/her displays on address-bar(URL).

Form POST Method

POST method is secured method because it hides all information.
Using POST method unlimited data sends . POST method is slower method comparatively GET method.

Submit Form using POST Method.

 

In the given above example:
user enters the name inside text box, after entered the name inside text box click on submit button it will display the name entered by user like user enters “Phptpoint” inside the text box the output displays “Phptpoint”.
In this example we have used Form POST method. So the user’s input doesn’t display on address-bar.

Submit Form using POST method(Sum of Two number).

 
In the given above example:
user enters the first number inside first text box and second number inside second text box, after entered the value inside the text box, clicked on “+” button.
The program displays the output Sum = addition of two numbers.

Create a Login Form (using POST Method)

 

In the given above example:
there is a secure login page. in which user enters the valid user_name and password, after entering the valid user_name and password he has to clicked on Sign-In button. authorized user can visit next page and for unauthorized user it shows an error message.

How to use HTML Form action

Action is used to give reference/link of another page.
If we want to separate the business logic (PHP script) from Presentation layer (HTML script) then use action Property of Form .
It reduce the complexity of bulk of codes. Because All scripts are separately define on their own page.
In the previous Form Post method PHP script and HTML script were define on the same page ,so it show the design part with the output of program.
But using action property HTML script define on a separate page and Business logic(PHP script) on another separate page.

Create HTML Form with action Property

Save it as DesingView.php

PHP Script

Save it as Logic.php
 
First we make Form using HTML script. We design a textbox to take input through user and a submit button with value(“show my name”) .
When name is entered by user and click on submit button the value of textbox redirect to php script page. Because Action Attribute is used here for link .
Information that is send by user is collect by using $_POST[] and store in a local variable($name).
Now a local variable is concatenate with String(“welcome”) and print, output will become Welcome Sanjeev.

No comments:

Post a Comment