TYBCA PHP Practical

/* Write a PHP program to create a simple calculator that can accept two numbers and perform operations like add, subtract, multiplication and divide (using Self Processing form) */
Calculator.php
<HTML>
<BODY>
<FORM method=POST action="<?php echo $_SERVER['PHP_SELF'] ?>">
Enter   First   Value  :<INPUT type=text name=a><BR><BR>
Enter Second Value:<INPUT type=text name=b><BR><BR>
<b>Select Arithmetic Operation</b><BR><BR>
<INPUT type=radio name="choice" value="1">Addition<BR><BR>
<INPUT type=radio name="choice" value="2">Subtraction<BR><BR>
<INPUT type=radio name="choice" value="3">Multiplication<BR><BR>
<INPUT type=radio name="choice" value="4">Division<BR><BR>
<INPUT type=submit name=submit value="Calculate"><BR>
</FORM>
<?php
         $c="";
         if(isset($_POST['submit']))
         {
           $a=$_POST['a'];
           $b=$_POST['b'];
           $ch=$_POST['choice'];
          
           if($ch=='1')
           {
            $c=$a+$b;
            echo"Addition =  $c";
           }
           if($ch=='2')
           {
            $c=$a-$b;
            echo"Subtraction =  $c";
           }
           if($ch=='3')
           {
            $c=$a*$b;
            echo"Multiplication =  $c";
           }
           if($ch=='4')
           {
            $c=$a/$b;
            echo "Division =  $c";
           }
         }
?>
</BODY>
</HTML>

















 /* Write a Calculator class that can accept two values, then add them, subtract them, multiply them together, or divide them on request. For example:
$calc = new Calculator( 3, 4 );
echo $calc- >add(); // Displays “7”
echo $calc- >multiply(); // Displays “12”        */
Calculate.html
<HTML>
<BODY>
<FORM method=get action="Calculate.php">
Enter first value: <INPUT type=text name="a"><br>
Enter second value:<INPUT type=text name="b"><br>
<INPUT type=submit>
</FORM>
</BODY>
</HTML>

Calculate.php
<?php
      class Calculate
      {
        public $a;
        public $b;
      
        function __construct($a,$b)
        {
          $this->a=$a;
          $this->b=$b;
         }
         public function add()
         {
          $c=$this->a+$this->b;
          echo"Addition = $c<br>";
         }
         public function subtract()
         {
          $c=$this->a-$this->b;
          echo"Subtract = $c<br>";
         }
         public function multiply()
         {
          $c=$this->a*$this->b;
          echo"Multiplication = $c<br>";
         }
         public function div()
         {
          $c=$this->a/$this->b;
          echo"Division = $c";
         }
    }
$x=$_GET['a'];
$y=$_GET['b'];
$calc=new Calculate($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();
?>








/* Create a form to accept employee details like name, address and mobile number. Once  the employee information is accepted, then accept LIC information like policy_no, name, premium. Display employee details and LIC details on next form.(use COOKIE)  */
Employee.html
<html>
<body>
<form method=get action="lic.php">
Enter Employee Name : <input type=text name=ename><br>
Enter Address : <input type=text name=address><br>
Enter Mobile No.<input type=text name=mobile><br>
<input type=submit value=Save>
</form>
</body>
</html>

Lic.php
<?php
       $ename=$_GET['ename'];
       $address=$_GET['address'];
       $mobile=$_GET['mobile'];
       setcookie("ename",$ename);
       setcookie("address",$address);
       setcookie("mobile",$mobile);
       echo"Hello $ename enter your LIC details <br><br>";
       echo"<form method=get action=display.php>
                 Policy No.:<input type=text name=pno><br>
                 Policy Name:<input type=text name=name><br>
                 Premium : <input type=text name=premium><br>
                 <input type=submit value=Display>
                </form>";
 ?>
     
Display.php
<?php
      $pno=$_GET['pno'];
      $name=$_GET['name'];
      $premium=$_GET['premium'];
    if(isset($_COOKIE['ename'])&&isset($_COOKIE['address'])&&isset($_COOKIE['mobile']))
   {
      echo"<br><b>Employee Details </b><br>";
     echo"Employee Name : ".$_COOKIE['ename']." <br>";
     echo"Employee Address :".$_COOKIE['address']."<br>";
     echo"Mobile No. : ".$_COOKIE['mobile']."<br>";
     echo"<br><b>LIC Policy Details</b><br>";
     echo"Policy No. : $pno<br>";
     echo"Policy Name : $name<br>";
     echo"Premium : $premium";
  }
?>















/ * Write a PHP Script to Upload the file and display its information.(use $_FILES).  */
FileUpload.html
<html>
<body>
<form method=post action="Uploadfile.php" enctype="multipart/form-data">
File Name : <input type="file" name="file" id="file" size=40%><br>
<input type=submit name=submit value=submit>
</form>
</body>
</html>

Uploadfile.php
<?php
          if($_FILES["file"]["error"]>0)
           {
             echo"Error : ".$_FILES["file"]["error"]."<br>";
           }
          else
           {
              echo"Upload File : ".$_FILES["file"]["name"]."<br>";
              echo"Type : ".$_FILES["file"]["type"]."<br>";
              echo"Size : ".($_FILES["file"]["size"]/1024)." kb<br>";
              echo"Temporary Storage : ".$_FILES["file"]["tmp_name"];
           }
?>            







/* Write a PHP Script to display Server information in table format (Use $_SERVER). */
ServerInfo.php
<?php
           echo"<table border=1>";
           foreach($_SERVER as $k=>$v)
           {
              echo"<tr><td>".$k."</td><td>".$v."</td></tr>";
           }
           echo"</table>";
?>






















/ *  Write a PHP script to accept a string and then display each word of string in reverse order.     (use concept of self processing form)   */
StringRev.php
<html>
<body>
<form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
Enter String : <input type=text name=str1><br>
<input type=submit name=submit>
</form>
<?php
            if(isset($_POST['submit']))
            {
              $str=$_POST['str1'];
              $nstr=strrev($str);
              echo"<br>".$nstr;
            }
?>
</body>
</html>

No comments:

Ebook

Ebook
Ebook