SlideShare a Scribd company logo
hasen@microcis.net July 12, 2013Hassen poreya
Trainer, Cresco Solution
Afghanistan Workforce
Development Program
PHP
Form Handling, and File Handling
Form Handling
 An HTML form is used to take the data from a user,
and then you can use those data that are held by
the form.
 Different inputs are available in a form that are
used to get input from a user.
 Text fields, text areas, selection lists, and radio
buttons, etc.
 When the user clicks the “submit” button, each of
these above becomes a string of text offered to the
Web server.
Note
 When a form is submitted, PHP turns each form
field into a variable.
 If you put two form variables on a page with the
same name, the second one may overwrite the
value of the first.
<form name=“login”
action=“index.php” method=“POST”>
</form>
GET
 The GET method is designed for retrieving
information, such as a document, an image, or the
results of a database query, from the server.
 The GET method is what a web browser uses when
the user types in a URL or clicks on a link.
POST
 The POST method is meant for posting confidential
information, such as a credit card numbers,
passwords or information that is to be stored in a
database, to the server.
 When the user submits a form, either the GET or POST
method can be used
Accessing Form variables
 $_GET, $_POST, $_REQUEST
 PHP super global associative arrays
 Hold information sent from an HTML form.
 PHP automatically places each form variable value
into:
 $_GET[form variable name],
 $_POST[form variable name],
 $_REQUEST[form variable name]
Accessing Form variables
 $_GET
 Hold information sent from an HTML form using GET
method: $_GET['var']
 $_POST
 Hold information sent from an HTML form using POST
method: $_POST['var']
 $_REQUEST
 The form data submitted using either POST or GET
method, are also available in $_REQUEST variables.
The Difference
 $_GET retrieves variables from the query string,
or your URL.
 $_POST retrieves variables from a POST method,
such as (generally) forms.
 $_REQUEST is a merging of $_GET and
$_POST where $_POST overrides $_GET.
 Good to use $_REQUEST on self referential
forms for validations. Hope that helps you out!
Example – info.php
<html>
<body>
<form action="welcome.php“
method="post">
Name: <input type="text"
name="name" />
Age: <input type="text"
name="age" />
<input type="submit" />
</form>
</body>
</html>
Example – welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?>years old.
</body>
</html>
Exercise
 Create a page that user can input First Name, Last
Name, Faculty, Age, etc. When a user clicks the
“Submit” button, the form data is sent to and
displayed on a different page. Use GET method, and
GET super global variable.
 Create a page that user input his name and birth
year, in another page his name should be printed
along with his age. Use GET method, and REQUEST
super global variable.
Attention!
 You should care about what a user is inputting in
your form field. You should always check user’s
inputs.
 There are some built-in functions that prevents
injections, and other kinds of attacks via from fields.
 Moreover, you can validate your HTML form with
Javascript as well as using PHP regular expression to
check what a user has entered.
 Empty fields shouldn’t be submitted. That’s why you
use isset() function to check whether a user
entered data into a form field or not.
File Handling
 After submitting a form, the data may be shown in
the screen, or inserted in a database or even stored
in a file.
 To store the data in a file you need to know some
functions listed below:
 Fopen(); -- open a file
 Fwrite(); -- write in a file
 Feof – represent end of the file
 Fgets(); -- get the strings out of a file
 Fgetc(); -- get the character out of a file
 Close(); -- close a file
File Opening modes
Modes Description
r Read only. Starts at the beginning of the file.
r+ Read/Write. Starts at the beginning of the file.
w Write only. Opens and clears the contents of file; or creates a new
file if it doesn't exist.
w+ Read/Write. Opens and clears the contents of file; or creates a new
file if it doesn't exist.
a Append. Opens and writes to the end of the file or creates a new file
if it doesn't exist.
a+ Read/Append. Preserves file content by writing to the end of the file.
x Write only. Creates a new file. Returns FALSE and an error if file
already exists.
x+ Read/Write. Creates a new file. Returns FALSE and an error if file
already exists.
Example
<?php
$file=fopen("welcome.txt", "rw") or exit("Unable
to open file!");
$write="Welcome to PHP file handling";
fwrite($file, $write);
?>
<?php
$file2=fopen("welcome.txt", "r");
while(!feof($file2)){
echo fgetc($file2). "<br />";
if (feof($file)) echo "End of file";
}
?>
Exercise
1. Create a user registration form, which takes
username and password from a user and store
them in a file. – register.php
2. Create a login interface – login.php
1. Username field
2. Password field
3. Login button
3. When a user enter the registered username, the
location should goes to another page “index.php”
header(“Location:index.php”);
hasen@microcis.net July 12, 2013Hassen poreya
Trainer, Cresco Solution
Any Questions!

More Related Content

What's hot (20)

PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
DOCX
Php files
kalyani66
 
PPTX
Php File Operations
Jamshid Hashimi
 
PPT
php file uploading
Purushottam Kumar
 
PPTX
Files in php
sana mateen
 
PPTX
Form Handling using PHP
Nisa Soomro
 
PPT
Files and Directories in PHP
Nicole Ryan
 
ODP
Php File Upload
Hiroaki Kawai
 
PDF
12th computer-application-unit-8-study-material-english-medium
Emmanuel Matric Higher Secondary School
 
PPTX
WEB PROGRAMMING
sweetysweety8
 
PPTX
Uploading a file with php
Muhamad Al Imran
 
PPT
[DSBW Spring 2010] Unit 10: XML and Web And beyond
Carles Farré
 
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
PDF
Full xml
Aravindharamanan S
 
PPTX
Chap 5 php files part 1
monikadeshmane
 
PDF
Html5 cheat sheet
Tharun Kumar
 
PPTX
Building Adaptive Payment Systems with YQL
Jonathan LeBlanc
 
PPTX
Data file operations in C++ Base
arJun_M
 
PPTX
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
phanleson
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
Php files
kalyani66
 
Php File Operations
Jamshid Hashimi
 
php file uploading
Purushottam Kumar
 
Files in php
sana mateen
 
Form Handling using PHP
Nisa Soomro
 
Files and Directories in PHP
Nicole Ryan
 
Php File Upload
Hiroaki Kawai
 
12th computer-application-unit-8-study-material-english-medium
Emmanuel Matric Higher Secondary School
 
WEB PROGRAMMING
sweetysweety8
 
Uploading a file with php
Muhamad Al Imran
 
[DSBW Spring 2010] Unit 10: XML and Web And beyond
Carles Farré
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
OXUS 20
 
Chap 5 php files part 1
monikadeshmane
 
Html5 cheat sheet
Tharun Kumar
 
Building Adaptive Payment Systems with YQL
Jonathan LeBlanc
 
Data file operations in C++ Base
arJun_M
 
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
phanleson
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 

Viewers also liked (15)

PDF
Web app development_my_sql_08
Hassen Poreya
 
PDF
Web app development_html_css_03
Hassen Poreya
 
PDF
Web app development_php_05
Hassen Poreya
 
PPTX
Learn to Code with JavaScript - Choose Your Own Adventures
Tessa Mero
 
PDF
Web app development_database_design_er-mapping_12
Hassen Poreya
 
PDF
Web app development_database_design_11
Hassen Poreya
 
PDF
Web app development_my_sql_09
Hassen Poreya
 
PDF
Web app development_php_04
Hassen Poreya
 
PDF
Web app development_php_06
Hassen Poreya
 
PDF
Web app development_database_design_10
Hassen Poreya
 
PPTX
CodeIgniter Practice
Jamshid Hashimi
 
PDF
Web app development_crud_13
Hassen Poreya
 
PDF
Web app development_cookies_sessions_14
Hassen Poreya
 
PDF
Web app development_html_02
Hassen Poreya
 
PDF
Web app development_html_01
Hassen Poreya
 
Web app development_my_sql_08
Hassen Poreya
 
Web app development_html_css_03
Hassen Poreya
 
Web app development_php_05
Hassen Poreya
 
Learn to Code with JavaScript - Choose Your Own Adventures
Tessa Mero
 
Web app development_database_design_er-mapping_12
Hassen Poreya
 
Web app development_database_design_11
Hassen Poreya
 
Web app development_my_sql_09
Hassen Poreya
 
Web app development_php_04
Hassen Poreya
 
Web app development_php_06
Hassen Poreya
 
Web app development_database_design_10
Hassen Poreya
 
CodeIgniter Practice
Jamshid Hashimi
 
Web app development_crud_13
Hassen Poreya
 
Web app development_cookies_sessions_14
Hassen Poreya
 
Web app development_html_02
Hassen Poreya
 
Web app development_html_01
Hassen Poreya
 
Ad

Similar to Web app development_php_07 (20)

PPTX
Working with data.pptx
SherinRappai
 
PPTX
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
PPTX
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
berihun18
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PDF
Making web forms using php
krishnapriya Tadepalli
 
PPT
Chapter 07 php forms handling
Dhani Ahmad
 
PPTX
2-Chapter Edit.pptx debret tabour university
alemunuruhak9
 
PPT
Class 6 - PHP Web Programming
Ahmed Swilam
 
PPT
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
PDF
web2_lec6.pdf
ssuser893014
 
ODP
Form Processing In Php
Harit Kothari
 
ODP
PHP BASIC PRESENTATION
krutitrivedi
 
DOCX
Php forms and validations by naveen kumar veligeti
Naveen Kumar Veligeti
 
PDF
Php, mysq lpart4(processing html form)
Subhasis Nayak
 
PPTX
forms.pptx
asmabagersh
 
PPT
Intro to php
Sp Singh
 
PPTX
Web Techniques like Cookies and Sessions
SonaliAbhang
 
PPTX
html forms and server side scripting
bantamlak dejene
 
PPT
Php mysql
Abu Bakar
 
Working with data.pptx
SherinRappai
 
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
cpbloger553
 
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
berihun18
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
Making web forms using php
krishnapriya Tadepalli
 
Chapter 07 php forms handling
Dhani Ahmad
 
2-Chapter Edit.pptx debret tabour university
alemunuruhak9
 
Class 6 - PHP Web Programming
Ahmed Swilam
 
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
web2_lec6.pdf
ssuser893014
 
Form Processing In Php
Harit Kothari
 
PHP BASIC PRESENTATION
krutitrivedi
 
Php forms and validations by naveen kumar veligeti
Naveen Kumar Veligeti
 
Php, mysq lpart4(processing html form)
Subhasis Nayak
 
forms.pptx
asmabagersh
 
Intro to php
Sp Singh
 
Web Techniques like Cookies and Sessions
SonaliAbhang
 
html forms and server side scripting
bantamlak dejene
 
Php mysql
Abu Bakar
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Digital Circuits, important subject in CS
contactparinay1
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

Web app development_php_07

  • 1. [email protected] July 12, 2013Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program PHP Form Handling, and File Handling
  • 2. Form Handling  An HTML form is used to take the data from a user, and then you can use those data that are held by the form.  Different inputs are available in a form that are used to get input from a user.  Text fields, text areas, selection lists, and radio buttons, etc.  When the user clicks the “submit” button, each of these above becomes a string of text offered to the Web server.
  • 3. Note  When a form is submitted, PHP turns each form field into a variable.  If you put two form variables on a page with the same name, the second one may overwrite the value of the first. <form name=“login” action=“index.php” method=“POST”> </form>
  • 4. GET  The GET method is designed for retrieving information, such as a document, an image, or the results of a database query, from the server.  The GET method is what a web browser uses when the user types in a URL or clicks on a link.
  • 5. POST  The POST method is meant for posting confidential information, such as a credit card numbers, passwords or information that is to be stored in a database, to the server.  When the user submits a form, either the GET or POST method can be used
  • 6. Accessing Form variables  $_GET, $_POST, $_REQUEST  PHP super global associative arrays  Hold information sent from an HTML form.  PHP automatically places each form variable value into:  $_GET[form variable name],  $_POST[form variable name],  $_REQUEST[form variable name]
  • 7. Accessing Form variables  $_GET  Hold information sent from an HTML form using GET method: $_GET['var']  $_POST  Hold information sent from an HTML form using POST method: $_POST['var']  $_REQUEST  The form data submitted using either POST or GET method, are also available in $_REQUEST variables.
  • 8. The Difference  $_GET retrieves variables from the query string, or your URL.  $_POST retrieves variables from a POST method, such as (generally) forms.  $_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET.  Good to use $_REQUEST on self referential forms for validations. Hope that helps you out!
  • 9. Example – info.php <html> <body> <form action="welcome.php“ method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
  • 10. Example – welcome.php <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?>years old. </body> </html>
  • 11. Exercise  Create a page that user can input First Name, Last Name, Faculty, Age, etc. When a user clicks the “Submit” button, the form data is sent to and displayed on a different page. Use GET method, and GET super global variable.  Create a page that user input his name and birth year, in another page his name should be printed along with his age. Use GET method, and REQUEST super global variable.
  • 12. Attention!  You should care about what a user is inputting in your form field. You should always check user’s inputs.  There are some built-in functions that prevents injections, and other kinds of attacks via from fields.  Moreover, you can validate your HTML form with Javascript as well as using PHP regular expression to check what a user has entered.  Empty fields shouldn’t be submitted. That’s why you use isset() function to check whether a user entered data into a form field or not.
  • 13. File Handling  After submitting a form, the data may be shown in the screen, or inserted in a database or even stored in a file.  To store the data in a file you need to know some functions listed below:  Fopen(); -- open a file  Fwrite(); -- write in a file  Feof – represent end of the file  Fgets(); -- get the strings out of a file  Fgetc(); -- get the character out of a file  Close(); -- close a file
  • 14. File Opening modes Modes Description r Read only. Starts at the beginning of the file. r+ Read/Write. Starts at the beginning of the file. w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist. w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist. a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist. a+ Read/Append. Preserves file content by writing to the end of the file. x Write only. Creates a new file. Returns FALSE and an error if file already exists. x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists.
  • 15. Example <?php $file=fopen("welcome.txt", "rw") or exit("Unable to open file!"); $write="Welcome to PHP file handling"; fwrite($file, $write); ?> <?php $file2=fopen("welcome.txt", "r"); while(!feof($file2)){ echo fgetc($file2). "<br />"; if (feof($file)) echo "End of file"; } ?>
  • 16. Exercise 1. Create a user registration form, which takes username and password from a user and store them in a file. – register.php 2. Create a login interface – login.php 1. Username field 2. Password field 3. Login button 3. When a user enter the registered username, the location should goes to another page “index.php” header(“Location:index.php”);
  • 17. [email protected] July 12, 2013Hassen poreya Trainer, Cresco Solution Any Questions!