Form Processing In Php

8,200 views
8,048 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,200
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Form Processing In Php

  1. 1. PHP – Form Processing in PHP Harit Kothari [email_address]
  2. 2. Agenda <ul><li>Reading data from a page </li></ul><ul><li>$_REQUEST, $_GET & $_POST </li></ul><ul><li>Isset() & array_key_exists() </li></ul><ul><li>Validating data </li></ul>
  3. 3. Reading data from a page <ul><li>HTML Forms </li></ul><ul><li>Use of text box, text area, radio button, check box </li></ul><ul><li>Data (inserted or selected as per above objects) can be sent either of 2 ways – GET or POST request </li></ul><ul><li>So, a request is like a box, in which selected items are packed and sent </li></ul>
  4. 4. Practical Applications <ul><li>You are developing a web based form to collect user data </li></ul><ul><li>User fills data and when Submit button is pressed, the data should be inserted in database, or something like that </li></ul><ul><li>User login management </li></ul>
  5. 5. $_REQUEST <ul><li>Represents HTTP request as an array </li></ul><ul><li>Is automatically created when a Get or POST is made, superset of $_GET & $_POST </li></ul><ul><li>Also contains $_COOKIES array </li></ul><ul><li>Request type can be checked by: $_SERVER['REQUEST_METHOD'] It should return either 'GET' or 'POST' </li></ul><ul><li>See an example next </li></ul>
  6. 6. $_REQUEST Example <ul><li>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']; ?> </li></ul>
  7. 7. $_GET <ul><li>Parameters are appended to URL </li></ul><ul><li>Less secure </li></ul><ul><li>URL length limitation </li></ul><ul><li>See an example next </li></ul>
  8. 8. $_GET Example <ul><li>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! </li></ul>
  9. 9. $_POST <ul><li>Parameters are encoded as request body </li></ul><ul><li>More secure </li></ul><ul><li>No length limitation </li></ul><ul><li>Mostly used </li></ul><ul><li>See an example next </li></ul>
  10. 10. $_POST Example <ul><li>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! </li></ul><ul><li>So, what's the difference? - apparently that, it includes name & age parameters in request body! </li></ul>
  11. 11. isset() <ul><li>Check a particular variable is set or not </li></ul><ul><li>Typically used to check existance, and so to give respective error message if the value is required </li></ul><ul><li>Example : if(isset($name) == true) echo “name entered”; else echo “please enter your name”; </li></ul>
  12. 12. array_key_exists() <ul><li>Similar to isset(), checks if value exists in array </li></ul><ul><li>Initially known as key_exists() </li></ul><ul><li>if (array_key_exists(key, array)) { ... } </li></ul><ul><li>Example : if (array_key_exists('name', $person)) echo &quot;name entered&quot;; else echo &quot;please enter your name&quot;; </li></ul>
  13. 13. Validating data <ul><li>User input via form, as we have seen </li></ul><ul><li>Accept data, check its validity and report to the user </li></ul><ul><li>How? See next! </li></ul>
  14. 14. Example 1 - simple <ul><li>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> </li></ul>
  15. 15. <ul><li>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”); ?> </li></ul>
  16. 16. Example 2 – Self Processing <ul><li><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 /> </li></ul>
  17. 17. <ul><li><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> </li></ul>
  18. 18. <ul><li><?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 } </li></ul>
  19. 19. <ul><li>} 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;; } } ?> </li></ul>
  20. 20. Whats next?

×