Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
- $num1 = $_POST['num1'];
- $opr = strtolower($_POST['opr']);
- $num2 = $_POST['num2'];
- if($opr==' ')
- $res = $num1+$num2;
- elseif($opr=='-')
- $res = $num1-$num2;
- elseif($opr=='x')
- $res = $num1*$num2;
- elseif($opr=='/')
- $res = $num1/$num2;
- echo $res;
- exit;
- }
- ?><!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Kalkulator</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
- <script>
- $(function(){
- $('.num').click(function(){
- var opr = $('#opr').val();
- var newval1 = $('#num1').val()+$(this).val();
- var newval2 = $('#num2').val()+$(this).val();
- if(opr!=''){
- $('#num2').val(newval2);
- $('#res').val(newval2);
- }else{
- $('#num1').val(newval1);
- $('#res').val(newval1);
- }
- })
- $('.assign').click(function(){
- var opr = $('#opr').val($(this).val());
- })
- $('.res').click(function(){
- var num1 = $('#num1').val();
- var opr = $('#opr').val();
- var num2 = $('#num2').val();
- $.ajax({
- type:'post',
- url:'kalkulator.php',
- data:'num1='+num1+'&opr='+opr+'&num2='+num2,
- success:function(a){
- $('#res').val(a);
- }
- })
- })
- $('.rem').click(function(){
- $('#num1').val('');
- $('#opr').val('');
- $('#num2').val('');
- $('#res').val('');
- })
- })
- </script>
- </head>
- <body>
- <form action="" method="post">
- <table border="2">
- <tr>
- <td colspan="4">
- <input type="hidden" id="num1" name="num1" placeholder="Number 1" />
- <input type="hidden" id="opr" name="opr" placeholder="Operator" />
- <input type="hidden" id="num2" name="num2" placeholder="Number 2" />
- <input type="text" name="result" id="res" placeholder="Result" /><input type="button" value="C" class="rem"/>
- </td>
- </tr>
- <tr>
- <td width="41"><input type="button" value="1" class="num"/></td>
- <td width="41"><input type="button" value="2" class="num"/></td>
- <td width="41"><input type="button" value="3" class="num"/></td>
- <td width="30"><input type="button" value="+" class="assign"/></td>
- </tr>
- <tr>
- <td><input type="button" value="4" class="num"/></td>
- <td><input type="button" value="5" class="num"/></td>
- <td><input type="button" value="6" class="num"/></td>
- <td><input type="button" value="-" class="assign"/></td>
- </tr>
- <tr>
- <td><input type="button" value="7" class="num"/></td>
- <td><input type="button" value="8" class="num"/></td>
- <td><input type="button" value="9" class="num"/></td>
- <td><input type="button" value="X" class="assign"/></td>
- </tr>
- <tr>
- <td><input type="button" value="." class="num"/></td>
- <td><input type="button" value="0" class="num"/></td>
- <td><input type="button" value="/" class="assign"/></td>
- <td><input type="button" value="=" class="res"/></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment