Saturday, December 24, 2011

Post Method in PHP

Defination
POST is the preferred method of form submission today,particularly in non-idempotent usage (those that will result in permanent changes),such as adding information to a database. The form data set is included in the body of the form when it is forwarded to the processing agent.No visible change to the URL will result according to the different data submitted.


Example
<html>
<body>
<?php
//isset is a function that test a variable to see whether it has been assigned a value

if (isset($_POST["submit"])
{
  if( $_GET["name"] || $_GET["age"] )
  {
     echo "Welcome ". $_GET['name']. "<br />";
     echo "You Rollno is ". $_GET['rollno']. ";
     exit();
  }
}
?>

  <form action="<?php $_PHP_SELF ?>" method="GET">
  Name: <input type="text" name="name" />
  Age: <input type="text" name="rollno" />
  <input type="submit" />
  </form>
</body>
</html>

QUE: difference between get and post?

GET:

http://www.test.com/index.htm?name1=value1&name2=value2
  • The GET method produces a long string that appears in your server logs, in the browser's Location: box.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can't be used to send binary data, like images or word documents, to the server.
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.
POST:
  • The POST method does not have any restriction on data size to be sent.
  • The POST method can be used to send ASCII as well as binary data.
  • The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
  • The PHP provides $_POST associative array to access all the sent information using GET method.
    For GET methodClick to View

Get Method in PHP

What is GET Method?
   The GET method passes arguments from one page to the next as part of the Uniform Resource Indication(URI) querystring. When used for form handling, GET appends the indicated variable name(s) and value(s) to the URL designated in the ACTION attribute with a question mark separator and submits the whole thing to the processing agent.


Example
<html>
<body>
  <form action="<?php $_PHP_SELF ?>" method="GET">
<?php
  if( $_GET["name"] || $_GET["age"] )
  {
     echo "Welcome ". $_GET['name']. "<br />";
     echo "You Rollno is. ". $_GET['rollno']. ";
     exit();
  }
?>

  Name: <input type="text" name="name" />
  Age: <input type="text" name="rollno" />
  <input type="submit" />
  </form>
</body>
</html>

For post method with difference: Click to View

Scientific Calculator in PHP



Calculator.php


<body bgcolor="#CCCCCC">
<form method="post">
Enter Number:<Input type="text" name="txt" />
<br >
<table>
<tr>


<td><input type="radio" name="rd" value="sqrt"/>sqrt</td>
<td><input type="radio" name="rd" value="square"/>square</td>
<td><input type="radio" name="rd" value="sin"/>sin</td>
<td><input type="radio" name="rd" value="cos"/>cos</td>
<td><input type="radio" name="rd" value="tan"/>tan</td>
</tr>
</table>
<br>
<input type="submit" name="Submit" value="Enter" />
</form>

<?php
if(isset($_POST['Submit']))
{
$a=$_POST['txt'];
$b=$_POST['rd'];
$c="square";
$d="sqrt";
$e="sin";
$f="cos";
$g="tan";


if(is_numeric($a))
{
switch($b):

case($c):
echo $a." ";
echo " square is:".($a * $a);
break;
case($d):
echo $a." ";
echo "square root is:".sqrt($a);
break;
case($e):
echo "sin(".$a.")=";
echo sin(deg2rad($a));
break;
case($f):
echo "cos(".$a.")=";
echo cos(deg2rad($a));
break;

case($g):
echo "tan(".$a.")=";
echo tan(deg2rad($a));
break;
               endswitch;
}
else
{
echo "Enter number";
}
}
?>
</body>

Define PHP

Definition: (Hyper Text Preprocessor)

PHP is an open source,serverside, HTML embedded web-scripting language that is compatible with all the major web servers. PHP enable you to embed code fragments in normal HTML pages-code that is interpreted as your pages are served up to users. PHP makes web pages to connect with server-side database.


Example


<html>
<head>
<title>
Example
</title>
</head>
<body>
<?php


print("Hello! this is my first php example");
   or
echo "Hello!this is my first php example";


?>
</body>


o/p
Hello! this is my first php example