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

PDF
Form handling in php
PPT
Php File Operations
PDF
Php file handling in Hindi
PDF
PHP file handling
PPT
Lecture7 form processing by okello erick
PPT
uptu web technology unit 2 Xml2
PPT
uptu web technology unit 2 Xml2
PPT
PHP - Introduction to File Handling with PHP
Form handling in php
Php File Operations
Php file handling in Hindi
PHP file handling
Lecture7 form processing by okello erick
uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
PHP - Introduction to File Handling with PHP

What's hot (20)

PPT
uptu web technology unit 2 Xml2
DOCX
Php files
PPTX
Php File Operations
PPT
php file uploading
PPTX
Files in php
PPTX
Form Handling using PHP
PPT
Files and Directories in PHP
ODP
Php File Upload
PDF
12th computer-application-unit-8-study-material-english-medium
PPTX
WEB PROGRAMMING
PPTX
Uploading a file with php
PPT
[DSBW Spring 2010] Unit 10: XML and Web And beyond
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
PDF
PPTX
Chap 5 php files part 1
PDF
Html5 cheat sheet
PPTX
Building Adaptive Payment Systems with YQL
PPTX
Data file operations in C++ Base
PPTX
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
PPT
Lecture 20 - File Handling
uptu web technology unit 2 Xml2
Php files
Php File Operations
php file uploading
Files in php
Form Handling using PHP
Files and Directories in PHP
Php File Upload
12th computer-application-unit-8-study-material-english-medium
WEB PROGRAMMING
Uploading a file with php
[DSBW Spring 2010] Unit 10: XML and Web And beyond
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Chap 5 php files part 1
Html5 cheat sheet
Building Adaptive Payment Systems with YQL
Data file operations in C++ Base
Xml For Dummies Chapter 14 Processing Xml it-slideshares.blogspot.com
Lecture 20 - File Handling
Ad

Viewers also liked (15)

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

Similar to Web app development_php_07 (20)

PPTX
Working with data.pptx
PPTX
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
PPTX
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PDF
Making web forms using php
PPT
Chapter 07 php forms handling
PPTX
WorkingwithFormsinPHPpptx__2024_10_17_19_07_07 2.pptx
PPTX
2-Chapter Edit.pptx debret tabour university
PPT
Class 6 - PHP Web Programming
PPT
PHP - Introduction to PHP Forms
PDF
web2_lec6.pdf
ODP
Form Processing In Php
ODP
PHP BASIC PRESENTATION
DOCX
Php forms and validations by naveen kumar veligeti
PDF
Php, mysq lpart4(processing html form)
PPTX
forms.pptx
PPT
Intro to php
PPTX
Web Techniques like Cookies and Sessions
PPTX
html forms and server side scripting
Working with data.pptx
Unit - III.pptxbgffhjxfjdfjfgjnsnsnshdhsjsksjsjsjsjsjsjsjsjsldksk
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
Quick beginner to Lower-Advanced guide/tutorial in PHP
Making web forms using php
Chapter 07 php forms handling
WorkingwithFormsinPHPpptx__2024_10_17_19_07_07 2.pptx
2-Chapter Edit.pptx debret tabour university
Class 6 - PHP Web Programming
PHP - Introduction to PHP Forms
web2_lec6.pdf
Form Processing In Php
PHP BASIC PRESENTATION
Php forms and validations by naveen kumar veligeti
Php, mysq lpart4(processing html form)
forms.pptx
Intro to php
Web Techniques like Cookies and Sessions
html forms and server side scripting

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Empathic Computing: Creating Shared Understanding
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
A comparative analysis of optical character recognition models for extracting...
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx

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!