Friday 10 June, 2011

Lesson 9 : Post back in PHP

Retweet this button on every post blogger
The method of page post back is quite  easy concept it trip back  on the page on which our code has been placed or in another terms we can say page call itself  for further operation.
Simple example of PHP post back : 
<?php
/*
php code for handling post back.
*/
if(isset($_POST['submit']))
{
    $first = $_POST['fname'];
    $last = $_POST['lname'];
   echo "Full nmae : ".$first." ".$last;
}

?>

<html>
<head><title>Handling post back in PHP</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post" name="form">
<center>
<table>
<tr>
<td>first name</td><td><input type="text" name="fname" /></td>
<tr><td>last  name</td><td><input type="text" name="lname" /></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="go.."</td></tr>
</tr>
</table>
</center>
,</form>
</body>
</html >

 this simple program defining PHP post back as by checking in the starting of the program for posted array variable submit if it got it setted then the further script will  execute .

Another simple example on PHP post back with checkbox input : 
<?php
if(isset($_POST['submit']))
{
    $firstdigit = $_POST['fdigit'];
   $lastdigit = $_POST['ldigit'];
 
  if(isset($_POST['add']))
  {
        $sum =  $firstdigit + $lastdigit ;
  }
 
   if(isset($_POST['mul']))
  {
        $multi =  $firstdigit * $lastdigit ;
  }  
  

}


?>


<html>
<head>
<title>PHP post back</title>
</head>
<body>
<table>
<tr>
<td>First digit</td><td><input type="text" name="fdigit" /></td>
</tr>
<tr>
<td>Second digit</td><td><input type="text" name="sdigit" /></td>
</tr>
<tr>
<td>&nbsp;</td><td>Add<input type="checkbox" name="add" <?php if(isset($_POST['add'])) echo "checked"; ?> />Multiply<input type="checkbox" name="mul" <?php if(isset($_POST['mul'])) echo "checked"; ?> /></td>
</tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="process" /></td></tr>

<tr>
<td>Results</td>
<td>&nbsp;</td>
</tr>



<tr>
<td>Addition</td>
<td><?php if(isset($sum)) echo $sum; ?></td>
</tr>


<tr>
<td>Multipliaction</td>
<td><?php if(isset($multi)) echo $multi; ?></td>
</tr>


</table>
</body>
</html>