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.
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
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.
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.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<html>
<head>
<title>get_browser</title>
<?php
error_reporting(1);
$x=$_GET['f'];
$y=$_GET['s'];
$z=$x+$y;
echo "Sum of two number = ".$z;
?>
</head>
<body bgcolor="sky color">
<form method="GET" >
<table border="1" bgcolor="green">
<tr>
<td>Enter your first number</td>
<td><input type="text" name="f"/></td>
</tr>
<tr>
<td>Enter your second number</td>
<td><input type="text" name="s"/></td>
</tr>
<tr align="center">
<td colspan="2" >
<input type="submit" value="+"/></td>
</tr>
</table>
</form>
</body>
</html>
|
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.
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<?php
error_reporting(1);
$id = $_POST['id'];
$pass = $_POST['pass'];
if(isset($_POST['signin']))
{
if($id=="Deep" && $pass=="Deep123")
{
header('location:http://www.phptpoint.com');
}
else
{
echo "<font color='red'>Invalid id or password</font>";
}
}
?>
<body>
<form method="post">
<table border="1" align="center">
<tr>
<td>Enter Your Id</td>
<td><input type="text" name="id"/>
</td>
</tr>
<tr>
<td>Enter Your Password</td>
<td><input type="password" name="pass"/>
</td>
</tr>
<tr>
<td><input type="submit" name="signin" value="SignIn"/>
</td>
</tr>
</table">
</form>
</body>
|
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