SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus de 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
 

Dernier

Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...HetalPathak10
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 

Dernier (20)

Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.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