The document discusses form processing in PHP, including how to read data from forms using $_REQUEST, $_GET, and $_POST superglobals and validate user input data before using it. It provides examples of validating data types and required fields, and processing forms using both GET and POST requests.
PHP – FormProcessing in PHP Harit Kothari [email_address]
2.
Agenda Reading datafrom a page $_REQUEST, $_GET & $_POST Isset() & array_key_exists() Validating data
3.
Reading data froma 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 Youare 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 HTTPrequest 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 Assumingthat 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 areappended to URL Less secure URL length limitation See an example next
8.
$_GET Example Ifyou are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
9.
$_POST Parameters areencoded as request body More secure No length limitation Mostly used See an example next
10.
$_POST Example Ifyou are accepting name and age from a form, you can give output like following: Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old! So, what's the difference? - apparently that, it includes name & age parameters in request body!
11.
isset() Check aparticular 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 toisset(), 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 "name entered"; else echo "please enter your name";
13.
Validating data Userinput 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, "1234567890") == 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”); ?>
<?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 "selected" if so function media_selected ($type) { global $media_type; if ($media_type == $type) { echo "selected"; } } ?>