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["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> 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["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!
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 "name entered"; else echo "please enter your name";
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, "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 }
} 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"; } } ?>
0 comments
Post a comment