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>

14 comments:

  1. thanks for example of query string

    ReplyDelete
  2. it was helpful and very clear thanks.what if i have a drop down list for example "user selects the country" do i need to do the same

    ReplyDelete
  3. Nice Blog.... I'm trying this code but there is a problem i have that when i send the data (post) to database and refresh the page then automatically the data is again goes to the database without my sending.
    please someone help me

    ReplyDelete

  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  5. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
    AWS training in sholinganallur
    AWS training in Tambaram
    AWS training in Velachery

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This post is so interactive and informative.keep update more information...
    IELTS Coaching in Tambaram
    IELTS Coaching in Chennai

    ReplyDelete
  8. Great post. keep sharing such a worthy information.
    SEO Training in Chennai
    SEO Course

    ReplyDelete
  9. This post is so useful and informative. Keep updating with more information.....
    Future Of Cyber Security
    Cyber Security Techniques

    ReplyDelete