Friday 11 December 2015

PHP FORMS

Form GET Method
  GET method is unsecured method because it display all information on address bar/ url.
By default method is get method. Using GET method limited data sends. GET method is faster way to send data.

<head>
<?php echo $_GET['n'];?>
<title>get_browser</title>
</head>
<body bgcolor="sky color">
<form method="GET">
<table border="1" bgcolor="green">
<tr>
  <td>Enter your name</td>
  <td><input type="text" name="n"/></td>
</tr>
<tr>
<td colspon="2" align="center">
  <input type="submit" value="show my name"/></td>
</tr>
</table>
</form>
 </body>  

Form post method in PHP
POST method is secured method because it hides all information.
Using POST method unlimited data sends .
POST method is slower method comparatively GET method.

Enter your name and click on submit button.
<head>
<?php
  echo $_POST['n'];
?>  
</head>
<body bgcolor="sky color">
<form method="post">
<table border="1" bgcolor="green">
<tr>   <td>Enter your name</td>
  <td><input type="text" name="n"/></td>
</tr>
<tr>
<td colspon="2" align="center">
<input type="submit" value="show my name"/></td>
</tr>
</table>
 </form>
 </body>

HTML form action
Action is used to give reference/link of another page.
If we want to separate the business logic (PHP script) from Presentation layer (HTML script) then we use Action attribute of Form .
It reduce the complexity of bulk of codes. Because All scripts are separately define on their own page.
In the previous Form Post method PHP script and HTML script were define on the same page ,so it show the design part with the output of program.
But using Action property HTML script define on a separate page and Business logic(PHP script) on another separate page.

index.php
<body>
<form method="post" action="Logic.php">
<table border="1" align="center">
<tr>
<td>Enter your name</td>
<td><input type="text" name="n"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="sub" value="SHOW MY NAME"/>
</td>
</tr>
</table>
</form>
</body>

Logic.php
<?php
$name=$_POST['n'];
echo "Welcome ".$name;
?>

Display result in textbox in PHP
<?php
$x=$_POST['fnum'];
$y=$_POST['snum'];
if(isset($_POST['add']))
{
  $sum=$x+$y;
  echo "Result:<input type='text' value='$sum'/>";
}
?>
<body>
<form method="post">
  Enter first number <input type="text" name="fnum"/>
  <hr/>
  Enter second number <input type="text" name="snum"/>
  <hr/>
  <input type="submit" name="snum" name="add" value="ADD"/>
</form>
</body>