Friday, 17 August 2012

Processing HTML data in PHP page

Hi friends,
In this post am going to explain how we process the data posted from HTML form in PHP page.
If you not aware of HTML forms u can go through my previous post 

The values posted from HTML form to php page can be retrieved in 3 ways:
1. Using $_GET  - This is used when the values are posted using method 'get' in HTML form tag.
   ex: if you have a HTML element with name='email'.

        <form method='get' action='targetpage.php'>

       <input type='text' name='email'>
        </form>
        
     In targetpage.php the value of 'email' field can be retrieved as $_GET['email'].

2. Using $_POST - This is used when the values are posted using method 'post' in HTML form tag.


   ex: if you have a HTML element with name='email'.

        <form method='post' action='targetpage.php'>

       <input type='text' name='email'>
        </form>


    In targetpage.php the value of 'email' field can be retrieved as $_POST['email'].

3. Using $_REQUEST - This is used when the values are posted using method 'post' or 'get' in HTML form tag. 

    In targetpage.php the value of 'email' field can be retrieved as $_REQUEST['email'].

Note: $_REQUEST variable can be used in both cases(for get and post also). So It is a better practice to use $_REQUEST for form data processing.


Up to know we learned how to get values from HTML form to php page. Now we'll move a little bit forward i.e using extract() function.


as per our knowledge we can get form values like $_GET['email'], $_POST['email'], and $_REQUEST['email'].


using extract() function makes it easier to use values as php variables.

you can refer description of extract() function  
for ex: 

<?php

 echo $_REQUEST['email'];    //prints the email value
?>

<?php

extract($_REQUEST);
echo $email;                     // prints the email value
?> 

the above two pieces of code does same thing i.e prints email value.

But using extract method makes coding easier,  when there is a need of using same variable in multiple places.


So, it is better practice to use extract() method to fetch values from $_REQUEST or $_POST or $_GET.

Here is the code for posting values from HTML form and processing them in php page
Form.html:
<html>
<head>

<script language="javascript">
function validate(){
var email=document.getElementById('email').value;
var pass= document.getElementById('pwd').value;
var atpos=email.indexOf("@");            // getting the position of '@' symbol in email
var dotpos=email.lastIndexOf(".");     // getting the position of last '.' symbol in email 
if(email=="")                                           // if email value is empty
{
        alert("please enter email id");                   // show an alert message
 document.getElementById('email').focus();    // place the cursor in email field
return false;                                                   // stop the execution and return false.
}
//checking @ and . positions
else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)   
{
alert("Not a valid e-mail address");
document.getElementById('email').focus();
                        return false;
}
else if(pass=="")                     //if password is empty
{
alert("please enter password");          
document.getElementById('pwd').focus();
return false;
}
  else if(pass.length<6)             //if password length is less than 6 chars
{
alert("please enter password");
document.getElementById('pwd').focus();
return false;
}
return true;                        // if all conditions are satisfied then return true.
}
</script>

</head>
<body>

<form action="targetpage.php" method="post" onsubmit="return validate()">
<table width="100%" align="center">
<tr><th>Email</th><td align="center"><input type="text" name="email" id="email" /></td></tr>
<tr><th>Password</th><td align="center"><input type="password" name="pwd" id="pwd" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="login" /></td></tr>
</table>
</form>

</body>
</html>
---------------------------------------------------------------------------------------
targetpage.php:
<?php
extract($_REQUEST);
echo "Email is: ".$email;
echo "Password is: ".$pwd;
?>

----------------------------------------------------------------------------------------

In my next post I'll explain you how to store values in database and  how to retrieve values from database.

Up to then Take Care bye.... 
Urs Venky..... :)

No comments:

Post a Comment