0

As part of my assignment I am required to make a calculator but MUST do the calculations in PHP. When the '=' button is clicked the numbers and operation stored need to be sent to PHP to calculate. Could somebody help me achieve this.

I currently have the calculations working in JQuery but need it to be in PHP instead.

$(".equals").click(function() {
var result;
number2 = parseFloat($result.val());

// Addition
if(operator === "+") {
    result = number1 + number2;
// Subtraction  
} else if (operator === "-") {
    result = number1 - number2;
// Multiplication   
} else if (operator === "*") {
    result = number1 * number2;
// Division
} else if (operator === "/") {
    result = number1 / number2;
}

    $result.val(result);
});
2
  • Where does operator and number1 come from? Commented Dec 12, 2014 at 2:44
  • @Scopey they're variables I've set for when someone clicks a number in the calculator it is stored in number1 and when they click an operator(+, -, / , *) it is stored in the operator variable. These will need to be posted to PHP somehow Commented Dec 12, 2014 at 2:46

3 Answers 3

1

if you are using jquery then you can utilize the features its $.ajax to post the data to the script. Just remember to set your http response codes and exits appropriatly and get the forms data serialized before sending it off to the PHP script.

                      var fData = $("your_form_selector").serialize()
                      $.ajax({
                      type: 'POST',
                      url: 'url_of_script.php',
                      data: fData
                  }).done(function(response){
                        alert(response);
                  }).fail(function(data) {
                    if (data.responseText !== '') {
                      alert(data.responseText);
                    } else {
                        /** DEBUGGING
                          var fObj = data;
                          for(var key in fObj) {
                          alert('key: ' + key + '\n' + 'value: ' + fObj[key]);
                         }                       **/
                    }
                    });

if you need to debug whats going on just uncomment the debugging and see what your dealing with.

if you have a look at the docs for it at Jquery ajax you can easily find all the information you need. Their docs are really good in my opinion.

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

0

Here's a solution to your problem:

if (isset($_POST) && !empty($_POST))
{
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $op   = $_POST['op'];


   $ans = eval("return $num1 $op $num2;");
   echo $ans;
}

Comments

0

First you need to grab the form field for operator in PHP through the $_POST array and then make a switch to go through your operators and assign a $result variable depending on the operator.

<?php

if ($_SERVER['REQUEST_METHOD'] == 'post'){
    $operator = $_POST['name or your operator input'];
    $num1 = $_POST['number1 named input'];
    $num2 = $_POST['number2 named input'];  
}

$result = 0;

switch($operator){
    case '+':
        $result = $num1 + $num2;
    break;
    case '-':
        $result = $num1 - $num2;
    break;
    case '/':
        $result = $num1 / $num2;
    break;
    case '*':
        $result = $num1 * $num2;
    break;
    default:
        echo "Invalid operator";
}
  http_response_code(200);
  echo $result;
  exit();
?>

1 Comment

Thank you! How can I connect this on the client side using $.post()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.