SartajPHP ( PHP Framework )


Calculator1

Calculator1

This page explain How can you develop an application with SartajPHP. This is calculator which is only calculate addition of two numbers. SartajPHP is a flexible development structure in nature. So There are so many way to create calculator application. We are using here an application and one Temp File. View Demo here http://www.sartajphp.com/hello2.html

Create an Application (hello.php) with Below Code--

class index extends \Sphp\tools\BasicApp{

    private $temp1 = null;

   // onstart event trigger on application start processing
    public function onstart(){
        global $masterf;
        // check user permission
        $this->getAuthenticate("ADMIN,GUEST");
        // set default DB table for components if application uses Database Table
       $this->setTableName("users");

       // create temp object to control and manage HTML output
        $this->temp1 = new \Sphp\tools\TempFile($this->apppath . "forms/calculator.front");

       // set master file for temp object, use if application output is html. Here we use global variable in comp file
        $this->setMasterFile($masterf);
    }

    public function page_new(){
        echo "my first application";
    }

   public function page_event_calc($evtp){
       // read SartajPHP component Values
       $add = floatval($this->temp1->txt1->value) + floatval($this->temp1->txt2->value);
     // reply back to browser as ajax json, it works if AJAX request receive
       $this->JSServer->addJSONBlock('html','out2',"Ajax Form Result is {$add}");
   }

   public function page_event_calc2($evtp){
       // read SartajPHP component Values
       $add = floatval($this->temp1->txt3->value) + floatval($this->temp1->txt4->value);
       // no master file or temp file use, simple echo
       echo "Your Result is {$add}";
   }
 
}

Create Temp File (calculator.php)

<p>Type Two Number and Click on Submit Button, 
This will add two number on server and result back to client
 via ajax form submit</p>
<div>Form Result is:
<div id="out2"></div>
</div>
<p><strong>&nbsp;</strong></p>
<form id="frm1" action="https://www.sartajphp.com/pages-calc.html">
<br>Number1: <input id="txt1" type="text">
<br>Number2: <input id="txt2" type="text">
<br><input type="submit" value="Calculate">
</form>
<p>&nbsp;</p>
<p>Type Two Number and Click on Button, 
This will add two number on server and result back 
to client via ajax<br><br>
Number1: <input id="txt3" type="text">
<br>Number2: <input id="txt4" type="text"><br>
<input type="button" value="Calculate">
<br><br></p>
<div id="out1"></div>

View Demo here http://www.sartajphp.com/hello2.html