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

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
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
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
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
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

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