arijulianto

Calculator dengan PHP + AJAX

Apr 14th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2. if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
  3. $num1 = $_POST['num1'];
  4. $opr = strtolower($_POST['opr']);
  5. $num2 = $_POST['num2'];
  6. if($opr==' ')
  7.     $res = $num1+$num2;
  8. elseif($opr=='-')
  9.     $res = $num1-$num2;
  10. elseif($opr=='x')
  11.     $res = $num1*$num2;
  12. elseif($opr=='/')
  13.     $res = $num1/$num2;
  14. echo $res;
  15. exit;
  16. }
  17.  
  18. ?><!doctype html>
  19. <html>
  20. <head>
  21. <meta charset="utf-8">
  22. <title>Kalkulator</title>
  23. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  24. <script>
  25. $(function(){
  26. $('.num').click(function(){
  27.     var opr = $('#opr').val();
  28.     var newval1 = $('#num1').val()+$(this).val();
  29.     var newval2 = $('#num2').val()+$(this).val();
  30.     if(opr!=''){
  31.         $('#num2').val(newval2);
  32.         $('#res').val(newval2);
  33.     }else{
  34.         $('#num1').val(newval1);
  35.         $('#res').val(newval1);
  36.     }
  37. })
  38. $('.assign').click(function(){
  39.     var opr = $('#opr').val($(this).val());
  40. })
  41. $('.res').click(function(){
  42.     var num1 = $('#num1').val();
  43.     var opr = $('#opr').val();
  44.     var num2 = $('#num2').val();
  45.     $.ajax({
  46.         type:'post',
  47.         url:'kalkulator.php',
  48.         data:'num1='+num1+'&opr='+opr+'&num2='+num2,
  49.         success:function(a){
  50.             $('#res').val(a);
  51.         }
  52.     })
  53. })
  54. $('.rem').click(function(){
  55.     $('#num1').val('');
  56.     $('#opr').val('');
  57.     $('#num2').val('');
  58.     $('#res').val('');
  59. })
  60. })
  61. </script>
  62. </head>
  63.  
  64. <body>
  65. <form action="" method="post">
  66. <table border="2">
  67. <tr>
  68. <td colspan="4">
  69. <input type="hidden" id="num1" name="num1" placeholder="Number 1" />
  70. <input type="hidden" id="opr" name="opr" placeholder="Operator" />
  71. <input type="hidden" id="num2" name="num2" placeholder="Number 2" />
  72. <input type="text" name="result" id="res" placeholder="Result" /><input type="button" value="C" class="rem"/>
  73.  
  74. </td>
  75. </tr>
  76. <tr>
  77. <td width="41"><input type="button" value="1" class="num"/></td>
  78. <td width="41"><input type="button" value="2" class="num"/></td>
  79. <td width="41"><input type="button" value="3" class="num"/></td>
  80. <td width="30"><input type="button" value="+" class="assign"/></td>
  81. </tr>
  82. <tr>
  83. <td><input type="button" value="4" class="num"/></td>
  84. <td><input type="button" value="5" class="num"/></td>
  85. <td><input type="button" value="6" class="num"/></td>
  86. <td><input type="button" value="-" class="assign"/></td>
  87. </tr>
  88. <tr>
  89. <td><input type="button" value="7" class="num"/></td>
  90. <td><input type="button" value="8" class="num"/></td>
  91. <td><input type="button" value="9" class="num"/></td>
  92. <td><input type="button" value="X" class="assign"/></td>
  93. </tr>
  94. <tr>
  95. <td><input type="button" value="." class="num"/></td>
  96. <td><input type="button" value="0" class="num"/></td>
  97. <td><input type="button" value="/" class="assign"/></td>
  98. <td><input type="button" value="=" class="res"/></td>
  99. </tr>
  100. </table>
  101. </form>
  102.  
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment