2017-01-09 5 views
0

私はPHPの初心者です。電卓を作成しました。PHPファイルに変数(q)として計算を掲示しています。計算を行い、結果としてボックスに戻したいと思います。 。PHP変数の計算

JavaScriptの '評価'のように私を助けることができる関数を見つけることができませんでした。

私が 'eval'を使用していないのは、計算がサーバー側で行われなければならないからです。

ご協力いただきますようお願い申し上げます。
http://www.w3schools.com/php/php_ref_math.asp

コールバック:あなたがここに関連するPHP関数のリストを見つけることができます

<!DOCTYPE html> 
<html > 
<head> 
    <meta charset="UTF-8"> 
    <title>calculator</title> 


    <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> 
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css'> 

     <link rel="stylesheet" href="css/style.css"> 

</head> 

<body> 

    <div class="container col-sm-2 text-center" id="calc"> 

     <div class="row"> 
     <form name="calc"> 
      <input type="text" class="screen text-right result" name="result" placeholder="0" readonly> 
     </form> 
     </div> 


    <!-- First row --> 
     <div class = "row"> 
     <button class="btn btn-primary number" data-key="1" type="button">1</button> 
     <button class="btn btn-primary number" data-key="2" type="button">2</button> 
     <button class="btn btn-primary number" data-key="3" type="button">3</button> 
     <button class="btn btn-danger" data-key="reset" type="button">C</button> 
     </div> 

     <!-- Second row --> 
     <div class = "row"> 
     <button class="btn btn-primary number" data-key="4" type="button">4</button> 
     <button class="btn btn-primary number" data-key="5" type="button">5</button> 
     <button class="btn btn-primary number" data-key="6" type="button">6</button> 
     <button class="btn btn-warning number" data-key="+" type="button">+</button> 
     </div> 

     <!-- Third row --> 
     <div class = "row"> 
     <button class="btn btn-primary number" data-key="7" type="button">7</button> 
     <button class="btn btn-primary number" data-key="8" type="button">8</button> 
     <button class="btn btn-primary number" data-key="9" type="button">9</button> 
     <button class="btn btn-warning number" data-key="-" type="button">-</button> 
     </div> 

     <!-- Fourth row--> 
     <div class = "row"> 
     <button class="btn btn-primary number" data-key="0" type="button">0</button> 
     <button class="btn btn-primary number" data-key="." type="button">.</button> 
     <button class="btn btn-warning number" data-key="/" type="button">/</button> 
     <button class="btn btn-warning number" data-key="*" type="button">*</button> 
     </div> 

     <div class="row"> 
     <button class="btn btn-success btn-lg equal" type="button">=</button> 
     </div> 

    </div> 

    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> 
    <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script> 

    <script src="js/calculator.js"></script> 

<!-- jquery fade in function --> 

<script> 

$("#calc").hide(500).delay(1500).show(300); 

</script> 


</body> 
</html> 
+1

コードなしで、それは助けるために、それはすべてのこと難しくなります。編集:あなたがそれを投稿するのにかかる時間が長いほど、(うまくいけば)解決策を取るのに時間がかかる。 –

+0

err ..計算機にしたい、あるいは 'php'で何らかの実行をしたくないのですか?おそらくあなたは 'javascript'に例を投稿することができますが、' php'には['eval']もあります(http://php.net/manual/en/function.eval.php) –

+0

@ Fred-ii - 。これが役に立ったら –

答えて

0

JAVASCRIPT

$('.equal').click(function(){ 
    $.post('calculator.php',{q: $('.result').val()} ,function(a){ 
    $('.result').val(a).show(); 
    }); 
}); 

PHP

<?php 

    $_POST[q]; 

?> 

HTML機能が役に立つかもしれません、ここでいくつかのドキュメントです:
http://php.net/manual/en/language.types.callable.php

コールバック関数の例:使用中

<?php 

// Just an example, two numbers passed with GET 
// $calc_choice will be add, subtract, etc. 

$num_one = $_GET['number_one']); 
$num_two = $_GET['number_two']); 
$calc_choice = $_GET['calculation']; 

// passing these variables into a callback function 
customEval($num_one, $num_two, $calc_choice); 

// This is abstraction 
// $callback is going to be a function we pass 
// You can name $numOne and $numTwo anything you would like 

function customEval($numOne, $numTwo, $callback){ 
    $answer = $callback($numOne, $numTwo); 
    echo '$answer'; 
} 

//This will add two numbers together and return them 
//What gets returns is what will be echoed in our callback function 
function add($numOne, $numTwo){ 
    return $numOne + $numTwo; 
} 

// You can make as many as you like 
function subtract($numOne, $numTwo){ 
    return $numOne - $numTwo; 
} 

?> 

$num_one = 5; 
$num_two = 10; 
$calc_choice = "add"; 

// our function would be doing this behind the scene 
customEval(5, 10, "add"){ 
    // Because our add function returns 15 in this case 
    echo '15' 
}