SlideShare a Scribd company logo
1 of 25
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
National Diploma in Information and Communication Technology
PHP :2-FORM-HANDLING>
K72C001M07 - Web Programming
11/23/2018 2-FORM-HANDLING 1
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTML Form
//login.html
<html>
<body>
<form action="login_get.php" method=“get">
Username: <input type="text" name="userName"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>
</body>
</html>
11/23/2018 2-FORM-HANDLING 2
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
When to use GET?
• Information sent from a form with the GET method is visible to
everyone .
• All variable names and values are displayed in the URL.
• GET also has limits on the amount of information to send.
• The limitation is about 2000 characters.
• The variables are displayed in the URL, it is possible to bookmark the
page.
• This can be useful in some cases.
• GET may be used for sending non-sensitive data.
• Note: GET should NEVER be used for sending passwords or other
sensitive information!
11/23/2018 2-FORM-HANDLING 3
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTTP GET method
//login_get.php
Welcome <?php echo $_GET["userName"]; ?>
<br> Your Password is: <?php echo
$_GET["password"];?>
11/23/2018 2-FORM-HANDLING 4
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
When to use POST?
• Information sent from a form with the POST method is invisible to
others.
• All names/values are embedded within the body of the HTTP
request.
• No limits on the amount of information to send.
• Supports advanced functionality such as support for multi-part binary
input while uploading files to server.
• It is not possible to bookmark the page.
• Developers prefer POST for sending form data.
11/23/2018 2-FORM-HANDLING 5
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTTP POST method
//login_post.php
Welcome <?php echo $_POST["userName"]; ?><br>
Your Password is: <?php echo
$_POST["password"];?>
11/23/2018 2-FORM-HANDLING 6
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (1): Form – Sign up
• Write a program to display entered details of following interface
• Method = POST
11/23/2018 2-FORM-HANDLING 7
Last Name
First Name
E-mail
Password
Conform Password
Sign up
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Form – Sign in
• Write a program to check user name and password correct or not.
• Give message successfully login or unauthorized access.
• Use your own user name and password
• Login.html
• Login.php
11/23/2018 2-FORM-HANDLING 8
User Name
Password
Sign in
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Answer
• Login.html
<html>
<body>
<h1>Login Form</h1>
<form method="POST" action=“Login.php">
<p> Name </p> <input type="text" name="name" size=20
/>
<p> Password </p> <input type="password" name="pass"
size=20 />
<input type="submit" name="login" value="login" />
</form>
</body>
</html
11/23/2018 2-FORM-HANDLING 9
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Answer
• Login.php
<?php
if(($_POST['name']=="user") &&
($_POST['pass']=="pass"))
echo "<h1> Hello ".$_POST['name']."</h1>";
else
echo "<h2> Access Denied </h2>";
?>
11/23/2018 2-FORM-HANDLING 10
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
isset( $var)
• Returns TRUE if var exists and has value other than NULL. FALSE
otherwise.
$var = '';
// This will evaluate to TRUE so the text will
be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
11/23/2018 2-FORM-HANDLING 11
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Values of Checked Checkboxes
• Example: checkbox.php
<h2>Select your technical exaposer:</h2>
<form action="#" method="post">
<input type="checkbox" name="check_list[]"
value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]"
value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]"
value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit"
value="Submit"/>
</form>
11/23/2018 2-FORM-HANDLING 12
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Values of Checked Checkboxes
• Example: checkbox.php
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}}}
?>
11/23/2018 2-FORM-HANDLING 13
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - single
• Example: select_option.php
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get
Selected Values" />
</form>
11/23/2018 2-FORM-HANDLING 14
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - single
• Example: select_option.php
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
echo "You have selected :" . $selected_val;
}
?>
11/23/2018 2-FORM-HANDLING 15
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - multiple
• Example: select_option_multiple.php
<form action="#" method="post">
<select name="Color[]" multiple>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get
Selected Values" />
</form>
11/23/2018 2-FORM-HANDLING 16
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - multiple
• Example: select_option_multiple.php
<?php
if(isset($_POST['submit'])){
foreach ($_POST['Color'] as $select)
{
echo "You have selected : $select <br/>";
}
}
?>
11/23/2018 2-FORM-HANDLING 17
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
• Example: signup.php
<html>
<head>
<title>Sign-Up</title>
<style>
body {
margin: auto;
width: 500px;
}
div {
padding: 10px;
}
div span {
color: red;
}
</style>
</head>
11/23/2018 2-FORM-HANDLING 18
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<body>
<h3>Registration Form</h3>
<?php
$nameErr = $userNameErr = $passwordErr =
$cpasswordErr= "";
$fullname = $email = $userName = $gender =
$password = $cpassword = null;
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
11/23/2018 2-FORM-HANDLING 19
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
$nameErr = "Name is required";
} else {
$fullname = $_POST['name'];
}
if (empty($_POST["user"])) {
$userNameErr = "Username is
required";
} else {
$userName = $_POST['user'];
}
11/23/2018 2-FORM-HANDLING 20
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
if (empty($_POST["pass"])) {
$passwordErr = "Password is
required";
} else {
$password = $_POST['pass'];
}
$email = $_POST['email'];
$gender = $_POST['gender'];
11/23/2018 2-FORM-HANDLING 21
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
$cpassword = $_POST['cpass'];
}
?>
<form method="POST" action="#">
<div>Name<input type="text" name="name"
/><span>*<?php echo $nameErr; ?></span></div>
<div>Email <input type="text"
name="email"></div>
<div>Gender:
<input type="radio" name="gender"
value="Female" checked>Female
<input type="radio" name="gender"
value="Male">Male</div>
11/23/2018 2-FORM-HANDLING 22
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<div>UserName <input type="text"
name="user"><span>*<?php echo $userNameErr;
?></span></div>
<div>Password <input type="password"
name="pass"><span>*<?php echo $passwordErr;
?></span></div>
<div>Confirm Password<input type="password"
name="cpass"></div>
<div><input id="button" type="submit"
name="submit" value="Sign-Up"></div>
</form>
<?php
echo'
<div>Name : '.$fullname.'</div>
11/23/2018 2-FORM-HANDLING 23
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<div>Email : '.$email.' </div>
<div>UserName : '.$userName.' </div>
<div>Gender : '.$gender.' </div>
<div>Password : '.$password.' </div>
<div>Confirm Password :
'.$cpassword.'</div>
';
?>
</body>
</html>
11/23/2018 2-FORM-HANDLING 24
Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Reference
www.w3schools.com
www.php.net
Friday, November 23, 2018 25

More Related Content

Similar to PHP Form Handling Guide

Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erickokelloerick
 
JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5Stephan Schmidt
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Step4 managementsendsorderw
Step4 managementsendsorderwStep4 managementsendsorderw
Step4 managementsendsorderwHüseyin Çakır
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMDean Hamstead
 
APEX connects Jira
APEX connects JiraAPEX connects Jira
APEX connects JiraOliver Lemm
 
Intro to Php Security
Intro to Php SecurityIntro to Php Security
Intro to Php SecurityDave Ross
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Roel Hartman
 
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplicationolegmmiller
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-KjaerCOMMON Europe
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Mohd Harris Ahmad Jaal
 

Similar to PHP Form Handling Guide (20)

Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Step4 managementsendsorderw
Step4 managementsendsorderwStep4 managementsendsorderw
Step4 managementsendsorderw
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PM
 
APEX connects Jira
APEX connects JiraAPEX connects Jira
APEX connects Jira
 
Intro to Php Security
Intro to Php SecurityIntro to Php Security
Intro to Php Security
 
Tshepo morailane(resume)
Tshepo morailane(resume)Tshepo morailane(resume)
Tshepo morailane(resume)
 
PHP-04-Forms.ppt
PHP-04-Forms.pptPHP-04-Forms.ppt
PHP-04-Forms.ppt
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
 
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplication
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Tutorial_4_PHP
Tutorial_4_PHPTutorial_4_PHP
Tutorial_4_PHP
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
PHP Security
PHP SecurityPHP Security
PHP Security
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
 

More from Achchuthan Yogarajah

Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Achchuthan Yogarajah
 
PADDY CULTIVATION MANAGEMENT SYSTEM
PADDY CULTIVATION MANAGEMENT  SYSTEMPADDY CULTIVATION MANAGEMENT  SYSTEM
PADDY CULTIVATION MANAGEMENT SYSTEMAchchuthan Yogarajah
 
Statistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationStatistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationAchchuthan Yogarajah
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y AchchuthanAchchuthan Yogarajah
 

More from Achchuthan Yogarajah (10)

Managing the design process
Managing the design processManaging the design process
Managing the design process
 
intoduction to network devices
intoduction to network devicesintoduction to network devices
intoduction to network devices
 
basic network concepts
basic network conceptsbasic network concepts
basic network concepts
 
4 php-advanced
4 php-advanced4 php-advanced
4 php-advanced
 
3 php-connect-to-my sql
3 php-connect-to-my sql3 php-connect-to-my sql
3 php-connect-to-my sql
 
Introduction to Web Programming
Introduction to Web Programming Introduction to Web Programming
Introduction to Web Programming
 
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
Language Localisation of Tamil using Statistical Machine Translation - ICTer2015
 
PADDY CULTIVATION MANAGEMENT SYSTEM
PADDY CULTIVATION MANAGEMENT  SYSTEMPADDY CULTIVATION MANAGEMENT  SYSTEM
PADDY CULTIVATION MANAGEMENT SYSTEM
 
Statistical Machine Translation for Language Localisation
Statistical Machine Translation for Language LocalisationStatistical Machine Translation for Language Localisation
Statistical Machine Translation for Language Localisation
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem  - by Y AchchuthanGreedy Knapsack Problem  - by Y Achchuthan
Greedy Knapsack Problem - by Y Achchuthan
 

Recently uploaded

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

PHP Form Handling Guide

  • 1. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology National Diploma in Information and Communication Technology PHP :2-FORM-HANDLING> K72C001M07 - Web Programming 11/23/2018 2-FORM-HANDLING 1
  • 2. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology HTML Form //login.html <html> <body> <form action="login_get.php" method=“get"> Username: <input type="text" name="userName"><br> Password: <input type="text" name="password"><br> <input type="submit"> </form> </body> </html> 11/23/2018 2-FORM-HANDLING 2
  • 3. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology When to use GET? • Information sent from a form with the GET method is visible to everyone . • All variable names and values are displayed in the URL. • GET also has limits on the amount of information to send. • The limitation is about 2000 characters. • The variables are displayed in the URL, it is possible to bookmark the page. • This can be useful in some cases. • GET may be used for sending non-sensitive data. • Note: GET should NEVER be used for sending passwords or other sensitive information! 11/23/2018 2-FORM-HANDLING 3
  • 4. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology HTTP GET method //login_get.php Welcome <?php echo $_GET["userName"]; ?> <br> Your Password is: <?php echo $_GET["password"];?> 11/23/2018 2-FORM-HANDLING 4
  • 5. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology When to use POST? • Information sent from a form with the POST method is invisible to others. • All names/values are embedded within the body of the HTTP request. • No limits on the amount of information to send. • Supports advanced functionality such as support for multi-part binary input while uploading files to server. • It is not possible to bookmark the page. • Developers prefer POST for sending form data. 11/23/2018 2-FORM-HANDLING 5
  • 6. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology HTTP POST method //login_post.php Welcome <?php echo $_POST["userName"]; ?><br> Your Password is: <?php echo $_POST["password"];?> 11/23/2018 2-FORM-HANDLING 6
  • 7. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Exercise (1): Form – Sign up • Write a program to display entered details of following interface • Method = POST 11/23/2018 2-FORM-HANDLING 7 Last Name First Name E-mail Password Conform Password Sign up
  • 8. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Exercise (2): Form – Sign in • Write a program to check user name and password correct or not. • Give message successfully login or unauthorized access. • Use your own user name and password • Login.html • Login.php 11/23/2018 2-FORM-HANDLING 8 User Name Password Sign in
  • 9. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Exercise (2): Answer • Login.html <html> <body> <h1>Login Form</h1> <form method="POST" action=“Login.php"> <p> Name </p> <input type="text" name="name" size=20 /> <p> Password </p> <input type="password" name="pass" size=20 /> <input type="submit" name="login" value="login" /> </form> </body> </html 11/23/2018 2-FORM-HANDLING 9
  • 10. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Exercise (2): Answer • Login.php <?php if(($_POST['name']=="user") && ($_POST['pass']=="pass")) echo "<h1> Hello ".$_POST['name']."</h1>"; else echo "<h2> Access Denied </h2>"; ?> 11/23/2018 2-FORM-HANDLING 10
  • 11. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology isset( $var) • Returns TRUE if var exists and has value other than NULL. FALSE otherwise. $var = ''; // This will evaluate to TRUE so the text will be printed. if (isset($var)) { echo "This var is set so I will print."; } $a = "test"; $b = "anothertest"; var_dump(isset($a)); // TRUE 11/23/2018 2-FORM-HANDLING 11
  • 12. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Values of Checked Checkboxes • Example: checkbox.php <h2>Select your technical exaposer:</h2> <form action="#" method="post"> <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/> <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/> <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/> <input type="submit" name="submit" value="Submit"/> </form> 11/23/2018 2-FORM-HANDLING 12
  • 13. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Values of Checked Checkboxes • Example: checkbox.php <?php if(isset($_POST['submit'])){ if(!empty($_POST['check_list'])){ foreach($_POST['check_list'] as $selected){ echo $selected."</br>"; }}} ?> 11/23/2018 2-FORM-HANDLING 13
  • 14. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Value of Select Option - single • Example: select_option.php <form action="#" method="post"> <select name="Color"> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> <option value="Pink">Pink</option> <option value="Yellow">Yellow</option> </select> <input type="submit" name="submit" value="Get Selected Values" /> </form> 11/23/2018 2-FORM-HANDLING 14
  • 15. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Value of Select Option - single • Example: select_option.php <?php if(isset($_POST['submit'])){ $selected_val = $_POST['Color']; echo "You have selected :" . $selected_val; } ?> 11/23/2018 2-FORM-HANDLING 15
  • 16. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Value of Select Option - multiple • Example: select_option_multiple.php <form action="#" method="post"> <select name="Color[]" multiple> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> <option value="Pink">Pink</option> <option value="Yellow">Yellow</option> </select> <input type="submit" name="submit" value="Get Selected Values" /> </form> 11/23/2018 2-FORM-HANDLING 16
  • 17. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Get Value of Select Option - multiple • Example: select_option_multiple.php <?php if(isset($_POST['submit'])){ foreach ($_POST['Color'] as $select) { echo "You have selected : $select <br/>"; } } ?> 11/23/2018 2-FORM-HANDLING 17
  • 18. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields • Example: signup.php <html> <head> <title>Sign-Up</title> <style> body { margin: auto; width: 500px; } div { padding: 10px; } div span { color: red; } </style> </head> 11/23/2018 2-FORM-HANDLING 18
  • 19. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields <body> <h3>Registration Form</h3> <?php $nameErr = $userNameErr = $passwordErr = $cpasswordErr= ""; $fullname = $email = $userName = $gender = $password = $cpassword = null; if(isset($_POST['submit'])){ if (empty($_POST["name"])) { 11/23/2018 2-FORM-HANDLING 19
  • 20. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields $nameErr = "Name is required"; } else { $fullname = $_POST['name']; } if (empty($_POST["user"])) { $userNameErr = "Username is required"; } else { $userName = $_POST['user']; } 11/23/2018 2-FORM-HANDLING 20
  • 21. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields if (empty($_POST["pass"])) { $passwordErr = "Password is required"; } else { $password = $_POST['pass']; } $email = $_POST['email']; $gender = $_POST['gender']; 11/23/2018 2-FORM-HANDLING 21
  • 22. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields $cpassword = $_POST['cpass']; } ?> <form method="POST" action="#"> <div>Name<input type="text" name="name" /><span>*<?php echo $nameErr; ?></span></div> <div>Email <input type="text" name="email"></div> <div>Gender: <input type="radio" name="gender" value="Female" checked>Female <input type="radio" name="gender" value="Male">Male</div> 11/23/2018 2-FORM-HANDLING 22
  • 23. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields <div>UserName <input type="text" name="user"><span>*<?php echo $userNameErr; ?></span></div> <div>Password <input type="password" name="pass"><span>*<?php echo $passwordErr; ?></span></div> <div>Confirm Password<input type="password" name="cpass"></div> <div><input id="button" type="submit" name="submit" value="Sign-Up"></div> </form> <?php echo' <div>Name : '.$fullname.'</div> 11/23/2018 2-FORM-HANDLING 23
  • 24. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Required Fields <div>Email : '.$email.' </div> <div>UserName : '.$userName.' </div> <div>Gender : '.$gender.' </div> <div>Password : '.$password.' </div> <div>Confirm Password : '.$cpassword.'</div> '; ?> </body> </html> 11/23/2018 2-FORM-HANDLING 24
  • 25. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Reference www.w3schools.com www.php.net Friday, November 23, 2018 25