SlideShare a Scribd company logo
PHP – Form Processing in PHP Harit Kothari [email_address]
Agenda Reading data from a page $_REQUEST, $_GET & $_POST Isset() & array_key_exists() Validating data
Reading data from a page HTML Forms Use of text box, text area, radio button, check box Data (inserted or selected as per above objects) can be sent either of 2 ways – GET or POST request So, a request is like a box, in which selected items are packed and sent
Practical Applications You are developing a web based form to collect user data User fills data and when Submit button is pressed, the data should be inserted in database, or something like that User login management
$_REQUEST Represents HTTP request as an array Is automatically created when a Get or POST is made, superset of $_GET & $_POST Also contains $_COOKIES array Request type can be checked by: $_SERVER['REQUEST_METHOD'] It should return either 'GET' or 'POST' See an example next
$_REQUEST Example Assuming that some data has been sent via a form. It can be interpreted in PHP as under: <?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { echo “This is a get request”; } else echo “This is a post request”; // extract values from $_REQUEST // uName & pWord areTextBoxs in HTML $username = $_REQUEST['uName']; $password = $_REQUEST['pWord']; ?>
$_GET Parameters are appended to URL Less secure URL length limitation See an example next
$_GET Example If you are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET[&quot;name&quot;]; ?>.<br /> You are <?php echo $_GET[&quot;age&quot;]; ?> years old!
$_POST Parameters are encoded as request body More secure No length limitation Mostly used See an example next
$_POST Example If you are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET[&quot;name&quot;]; ?>.<br /> You are <?php echo $_GET[&quot;age&quot;]; ?> years old! So, what's the difference? - apparently that, it includes name & age parameters in request body!
isset() Check a particular variable is set or not Typically used to check existance, and so to give respective error message if the value is required Example : if(isset($name) == true) echo “name entered”; else echo “please enter your name”;
array_key_exists() Similar to isset(), checks if value exists in array Initially known as key_exists() if (array_key_exists(key, array)) { ... } Example : if (array_key_exists('name', $person)) echo &quot;name entered&quot;; else echo &quot;please enter your name&quot;;
Validating data User input via form, as we have seen Accept data, check its validity and report to the user How? See next!
Example 1 - simple index.htm <html> <body> <form name =”test_form1” method = “POST” action = “form_process.php”> <p/>User Name <input type = “text” name = “name”/> <br/><p>User Name <input type = “text” name = “age”/> <br><input type = “submit” value = “Validate Me”/> </form> <body> </html>
form_process.php <?php $name = $_POST['name']; $age = $_POST['age']; // validity of age $valid_age = strspn($age, &quot;1234567890&quot;) == strlen($age); // above can be achieved also, as under: // $valid_age = preg_match('/^\d+$/', $age); if(!isset($name)) die(“Name is not given!”); if(!$valid_age) die(“Age is invalid”); ?>
Example 2 – Self Processing <html> <body> <form action=&quot;<?= $PHP_SELF ?>&quot; method=&quot;POST&quot;> Name: <input type=text name=&quot;name&quot; value=&quot;<?= $name ?>&quot; /><br /> Status: <input type=&quot;checkbox&quot; name=&quot;status&quot; value=&quot;active&quot; <?php if($status == 'active') { echo 'checked'; } ?> /> Active<br/> Media: <select name=&quot;media_type&quot;> <option value=&quot;&quot;>Choose one</option> <option value=&quot;picture&quot; <?php media_selected('picture') ?>  />Picture</optio <option value=&quot;audio&quot; <?php media_selected('audio') ?>  />Audio</option> <option value=&quot;movie&quot; <?php media_selected('movie') ?>  />Movie</option> </select><br /> File: <input type=&quot;text&quot; name=&quot;filename&quot; value=&quot;<?= $filename ?>&quot;  /><br />Caption: <textarea name=&quot;caption&quot;><?= $caption ? ></textarea><br />
<input type=&quot;hidden&quot; name=&quot;tried&quot; value=&quot;yes&quot; /> <input type=&quot;submit&quot; value=&quot;<?php echo $tried ? 'Continue' : 'Create'; ? >&quot; /> </form> </body> </html>
<?php $name = $_POST['name']; $media_type = $_POST['media_type']; $filename = $_POST['filename']; $caption = $_POST['caption']; $tried = ($_POST['tried'] == 'yes'); if ($tried)  { $validated = (!empty($name) && !empty($media_type) && ! empty($filename)); if (!$validated)  { ?> <p> The name, media type, and filename are required fields. Please  fill them out to continue. </p> <?php }
} if ($tried && $validated)  { echo '<p>The item has been created.</p>'; } // was this type of media selected? print &quot;selected&quot; if so function media_selected ($type)  { global $media_type; if ($media_type == $type) { echo &quot;selected&quot;;  } } ?>
Whats next?

More Related Content

What's hot (20)

PPTX
Xml presentation
Miguel Angel Teheran Garcia
 
PDF
Hotel Management System
Adil Ayyaz
 
PPTX
Object oriented database concepts
Temesgenthanks
 
PPTX
dbms lecture 1.pptx , where traditional file system vs database management ar...
dbmscse61
 
PPT
6. Integrity and Security in DBMS
koolkampus
 
PPTX
Characteristic of dabase approach
Luina Pani
 
PPT
PHP variables
Siddique Ibrahim
 
PPTX
weak slot and filler structure
Amey Kerkar
 
PPTX
lazy learners and other classication methods
rajshreemuthiah
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PDF
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
PPTX
Rdbms
rdbms
 
PPS
Java rmi
kamal kotecha
 
PDF
1 introduction databases and database users
Kumar
 
PPT
Fundamentals of Database ppt ch01
Jotham Gadot
 
PPTX
Structure of dbms
Megha yadav
 
PPTX
Cascading style sheets (CSS-Web Technology)
Timbal Mayank
 
PDF
2 database system concepts and architecture
Kumar
 
PPTX
Chapter-7 Relational Calculus
Kunal Anand
 
PPT
SQL : introduction
Shakila Mahjabin
 
Xml presentation
Miguel Angel Teheran Garcia
 
Hotel Management System
Adil Ayyaz
 
Object oriented database concepts
Temesgenthanks
 
dbms lecture 1.pptx , where traditional file system vs database management ar...
dbmscse61
 
6. Integrity and Security in DBMS
koolkampus
 
Characteristic of dabase approach
Luina Pani
 
PHP variables
Siddique Ibrahim
 
weak slot and filler structure
Amey Kerkar
 
lazy learners and other classication methods
rajshreemuthiah
 
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
Rdbms
rdbms
 
Java rmi
kamal kotecha
 
1 introduction databases and database users
Kumar
 
Fundamentals of Database ppt ch01
Jotham Gadot
 
Structure of dbms
Megha yadav
 
Cascading style sheets (CSS-Web Technology)
Timbal Mayank
 
2 database system concepts and architecture
Kumar
 
Chapter-7 Relational Calculus
Kunal Anand
 
SQL : introduction
Shakila Mahjabin
 

Similar to Form Processing In Php (20)

PPT
Php Basic Security
mussawir20
 
PPS
Php security3895
PrinceGuru MS
 
PPS
PHP Security
manugoel2003
 
PPT
Php Calling Operators
mussawir20
 
PPS
Php Security3895
Aung Khant
 
PPT
High-level Web Testing
petersergeant
 
PPT
Web Scraping with PHP
Matthew Turland
 
PPT
Php Sessoins N Cookies
mussawir20
 
PPT
Php Crash Course
mussawir20
 
ODP
HTML::FormHandler
bbeeley
 
PPT
Zend framework 04 - forms
Tricode (part of Dept)
 
ODP
Modern Web Development with Perl
Dave Cross
 
PPT
Exploiting Php With Php
Jeremy Coates
 
PPT
Drupal Lightning FAPI Jumpstart
guestfd47e4c7
 
PDF
Web Scraping with PHP
Matthew Turland
 
PPT
12-security.ppt - PHP and Arabic Language - Index
webhostingguy
 
Php Basic Security
mussawir20
 
Php security3895
PrinceGuru MS
 
PHP Security
manugoel2003
 
Php Calling Operators
mussawir20
 
Php Security3895
Aung Khant
 
High-level Web Testing
petersergeant
 
Web Scraping with PHP
Matthew Turland
 
Php Sessoins N Cookies
mussawir20
 
Php Crash Course
mussawir20
 
HTML::FormHandler
bbeeley
 
Zend framework 04 - forms
Tricode (part of Dept)
 
Modern Web Development with Perl
Dave Cross
 
Exploiting Php With Php
Jeremy Coates
 
Drupal Lightning FAPI Jumpstart
guestfd47e4c7
 
Web Scraping with PHP
Matthew Turland
 
12-security.ppt - PHP and Arabic Language - Index
webhostingguy
 
Ad

More from Harit Kothari (9)

PPTX
Key areas for successful software delivery
Harit Kothari
 
PPTX
Basic Intro to iOS
Harit Kothari
 
PPT
Introduction to Handoff
Harit Kothari
 
ODP
Free & Open Source - an introduction
Harit Kothari
 
PPT
OWASP Top 10 : Let’s know & solve
Harit Kothari
 
ODP
Session Management & Cookies In Php
Harit Kothari
 
ODP
Coding In Php
Harit Kothari
 
ODP
Database Connection With Mysql
Harit Kothari
 
ODP
Starting With Php
Harit Kothari
 
Key areas for successful software delivery
Harit Kothari
 
Basic Intro to iOS
Harit Kothari
 
Introduction to Handoff
Harit Kothari
 
Free & Open Source - an introduction
Harit Kothari
 
OWASP Top 10 : Let’s know & solve
Harit Kothari
 
Session Management & Cookies In Php
Harit Kothari
 
Coding In Php
Harit Kothari
 
Database Connection With Mysql
Harit Kothari
 
Starting With Php
Harit Kothari
 
Ad

Recently uploaded (20)

PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
The Future of Artificial Intelligence (AI)
Mukul
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 

Form Processing In Php

  • 1. PHP – Form Processing in PHP Harit Kothari [email_address]
  • 2. Agenda Reading data from a page $_REQUEST, $_GET & $_POST Isset() & array_key_exists() Validating data
  • 3. Reading data from a page HTML Forms Use of text box, text area, radio button, check box Data (inserted or selected as per above objects) can be sent either of 2 ways – GET or POST request So, a request is like a box, in which selected items are packed and sent
  • 4. Practical Applications You are developing a web based form to collect user data User fills data and when Submit button is pressed, the data should be inserted in database, or something like that User login management
  • 5. $_REQUEST Represents HTTP request as an array Is automatically created when a Get or POST is made, superset of $_GET & $_POST Also contains $_COOKIES array Request type can be checked by: $_SERVER['REQUEST_METHOD'] It should return either 'GET' or 'POST' See an example next
  • 6. $_REQUEST Example Assuming that some data has been sent via a form. It can be interpreted in PHP as under: <?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { echo “This is a get request”; } else echo “This is a post request”; // extract values from $_REQUEST // uName & pWord areTextBoxs in HTML $username = $_REQUEST['uName']; $password = $_REQUEST['pWord']; ?>
  • 7. $_GET Parameters are appended to URL Less secure URL length limitation See an example next
  • 8. $_GET Example If you are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET[&quot;name&quot;]; ?>.<br /> You are <?php echo $_GET[&quot;age&quot;]; ?> years old!
  • 9. $_POST Parameters are encoded as request body More secure No length limitation Mostly used See an example next
  • 10. $_POST Example If you are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET[&quot;name&quot;]; ?>.<br /> You are <?php echo $_GET[&quot;age&quot;]; ?> years old! So, what's the difference? - apparently that, it includes name & age parameters in request body!
  • 11. isset() Check a particular variable is set or not Typically used to check existance, and so to give respective error message if the value is required Example : if(isset($name) == true) echo “name entered”; else echo “please enter your name”;
  • 12. array_key_exists() Similar to isset(), checks if value exists in array Initially known as key_exists() if (array_key_exists(key, array)) { ... } Example : if (array_key_exists('name', $person)) echo &quot;name entered&quot;; else echo &quot;please enter your name&quot;;
  • 13. Validating data User input via form, as we have seen Accept data, check its validity and report to the user How? See next!
  • 14. Example 1 - simple index.htm <html> <body> <form name =”test_form1” method = “POST” action = “form_process.php”> <p/>User Name <input type = “text” name = “name”/> <br/><p>User Name <input type = “text” name = “age”/> <br><input type = “submit” value = “Validate Me”/> </form> <body> </html>
  • 15. form_process.php <?php $name = $_POST['name']; $age = $_POST['age']; // validity of age $valid_age = strspn($age, &quot;1234567890&quot;) == strlen($age); // above can be achieved also, as under: // $valid_age = preg_match('/^\d+$/', $age); if(!isset($name)) die(“Name is not given!”); if(!$valid_age) die(“Age is invalid”); ?>
  • 16. Example 2 – Self Processing <html> <body> <form action=&quot;<?= $PHP_SELF ?>&quot; method=&quot;POST&quot;> Name: <input type=text name=&quot;name&quot; value=&quot;<?= $name ?>&quot; /><br /> Status: <input type=&quot;checkbox&quot; name=&quot;status&quot; value=&quot;active&quot; <?php if($status == 'active') { echo 'checked'; } ?> /> Active<br/> Media: <select name=&quot;media_type&quot;> <option value=&quot;&quot;>Choose one</option> <option value=&quot;picture&quot; <?php media_selected('picture') ?> />Picture</optio <option value=&quot;audio&quot; <?php media_selected('audio') ?> />Audio</option> <option value=&quot;movie&quot; <?php media_selected('movie') ?> />Movie</option> </select><br /> File: <input type=&quot;text&quot; name=&quot;filename&quot; value=&quot;<?= $filename ?>&quot; /><br />Caption: <textarea name=&quot;caption&quot;><?= $caption ? ></textarea><br />
  • 17. <input type=&quot;hidden&quot; name=&quot;tried&quot; value=&quot;yes&quot; /> <input type=&quot;submit&quot; value=&quot;<?php echo $tried ? 'Continue' : 'Create'; ? >&quot; /> </form> </body> </html>
  • 18. <?php $name = $_POST['name']; $media_type = $_POST['media_type']; $filename = $_POST['filename']; $caption = $_POST['caption']; $tried = ($_POST['tried'] == 'yes'); if ($tried) { $validated = (!empty($name) && !empty($media_type) && ! empty($filename)); if (!$validated) { ?> <p> The name, media type, and filename are required fields. Please fill them out to continue. </p> <?php }
  • 19. } if ($tried && $validated) { echo '<p>The item has been created.</p>'; } // was this type of media selected? print &quot;selected&quot; if so function media_selected ($type) { global $media_type; if ($media_type == $type) { echo &quot;selected&quot;; } } ?>