0

I want to make a calculation and add button to store the First operand and the "+" then remove all the number so i can input the second operand. Than input the second operand. After that click button to send First operand , second operand and "+" to php.

It is the demo

i haven't finish the php code and it is just a "test" code to alert out the result.

My code is not working in some reason i don't know.

    <?
php print_r($_POST);
?>
yoadle
  • 188
  • 1
  • 2
  • 12
  • There's no such function as `alert` in PHP. – Josiah Keller Dec 14 '14 at 19:10
  • So what is your question? Do you need help with the javascript/jQuery or php? note, your php should be more like `` rather than using js `alert()` if you just want to show the result. – Sean Dec 14 '14 at 19:19
  • also your jquery post should most likely be `$.post("calculation.php", {execute: memory, ...` as you want to send your array, not just the current value. – Sean Dec 14 '14 at 19:25
  • @Sean i need help in the jQuery code because i m not sure the variable that i store is pass to php or not. As i mention, I need to send 2 data to php. I m not sure I m doing it right or not. – yoadle Dec 14 '14 at 19:28
  • See my 2nd comment on passing to php, you want `$.post("calculation.php", {execute: memory, ...` instead of `$.post("calculation.php", {execute: $show.val(), ...` as you want to send your `memory` array instead of just the current value `$show.val()`. – Sean Dec 14 '14 at 19:34
  • can you help me to modify my php code to see what i have post to php? – yoadle Dec 14 '14 at 19:39
  • If you want to see what you posted to php just do ``. Then you can see what you posted, and the format that it is in. – Sean Dec 14 '14 at 19:41
  • @Sean I just try that and it doesn't work. So i guess it is the jQuery code problem – yoadle Dec 14 '14 at 19:43
  • Can you expand on 'doesn't work'? Can you also provide an example of what you want to post to php and what you want to return from php? – Sean Dec 14 '14 at 19:48
  • The js and html is in the fiddle : http://jsfiddle.net/NicholasLui15/9t78jd47/2/ – yoadle Dec 14 '14 at 19:59
  • @Sean my problem is my add function is not working as i want. Also the php code is just . Actually, i want to use php to do calculation but I don't know what my jQuery will post to php. So i didn't do it yet. – yoadle Dec 14 '14 at 20:01
  • 1
    As a warning, do not suggest edits that vandalize the answers here. If you do this again, we will suspend your account. – Brad Larson Dec 15 '14 at 16:48

1 Answers1

0

There are a few edits you need to make to your js code.

(1) in your $(document).on('click', 'button.number', function () { you need to check if the current value is a .function (operator), and if so add the operator to memory before setting the value to the number. I used $.inArray($show.val(),['+','-','x','/']) !== -1 which checks to see if it is in the array.
(2) changing your $("#add").click(function () { to $(document).on('click', 'button.function', function () { allows you to use all your .function (operator) buttons for this code, and not just the +.
(3) you need to add the current value to memory before you post memory to your php.
(4) you need to change execute: $.show.val() to execute: memory

$(function () {
var $show = $('#display');
var currentDisplay = "";

$show.val(0);


$(document).on('click', 'button.number', function () {

    if ($show.val().length >= 8) {
        $show.val("Error");
    } else if ($show.val() == "Error") {
        $show.val("0");
    } 

    // (1) check if current value is a .function
    else if ($.inArray($show.val(),['+','-','x','/']) !== -1) {       
        var addOp = $show.val();
        if (addOp) memory.push(addOp);
        $show.val($(this).val());
    } 

    else {
        $show.val(($show.val() == "0" ? "" : $show.val()) + $(this).val());

    }
});
$("#clear").click(function () {
    $show.val("0");
});
$("#ce").click(function () {
    if ($show.val().length >= 2) {
        $show.val($show.val().substring(0, $show.val().length - 1));
    } else {
        $("#ce").trigger("click");
    }
});
var memory = [];

// (2) changed from $("#add").click(function () { so all functions are registered
$(document).on('click', 'button.function', function () {
    var addnum = $show.val();
    if (addnum) memory.push(addnum);
    $show.val($(this).val());
});

$('#equ').click(function () {

    // (3) add current value to memory
    var addnum = $show.val();
    memory.push(addnum);

    // (4) change execute: $show.val() to execute: memory
    $.post("calculation.php", {
        execute: memory
    }, function (data,status) {

        $show.val(data);
        var e = memory.join('');
        memory = [];

    });
});
});

Then in calculation.php you want to loop through the array and add/subtract depending on the operator.

<?php
if(isset($_POST['execute']) && is_array($_POST['execute'])) {
    $total = (int)$_POST['execute'][0];
    for($i=1;$i<count($_POST['execute']);$i+=2){
        switch($_POST['execute'][$i]){
            case '+': $total += (int)$_POST['execute'][$i+1];break;
            case '-': $total -= (int)$_POST['execute'][$i+1];break;
            default : $total += (int)$_POST['execute'][$i+1];
        }
    }
    echo $total;
}
else echo 'Error';
?>

(note: I only did + and - in php, assuming that you could figure out how to add the * and /)

here is an updated jsFiddle - http://jsfiddle.net/9t78jd47/3/ (it uses js code, instead of $.post() to mimic sending memory to calculation.php

Sean
  • 12,443
  • 3
  • 29
  • 47
  • thankyou for ur help but when i have do 3+3 =6 and i press +3 again it come up with Nan not 9 – yoadle Dec 14 '14 at 22:04
  • I forgot to add `memory = [];` to the js version. It is in the `$.post()`/php version. Here is an updated jsFiddle with `memory = [];` after `$show.val(total);` - http://jsfiddle.net/9t78jd47/4/ – Sean Dec 14 '14 at 22:07
  • i have tried to combine those code but it is not working it is the same problem of posting the value ? or it is because ur jsfiddle js part is only work with js but not php? – yoadle Dec 14 '14 at 22:20
  • If copied the jsFiddle example, and you want to use the php/`$.post` version you need to uncomment that section, and comment out the javascript version inside the `$('#equ').click(function () {` – Sean Dec 14 '14 at 22:35
  • i can see the javascript version is working but i m not using javascript to execute the maths. I want to use the php part to execute it. So i tried to remove the js part in my code and post it php but it is not working. What happen? – yoadle Dec 14 '14 at 22:48
  • without seeing you page I have no idea. note, I was missing a `)` at the end of `if(isset($_POST['execute']) && is_array($_POST['execute'])` in my php code example. Have you looked at your console.log/browser console/developer tools to make sure that the `$.post` occurred and see what the php code returned? – Sean Dec 14 '14 at 22:51
  • I used chrome dev tool and i checked. There is no request. There are only Request URL:file:///C:/xxx/xxx/xxxx/xxx/script.js so i guess the post method is not working? – yoadle Dec 14 '14 at 23:15
  • then you need to debug why `$.post` is not being called, which may include if `$('#equ').click(function () {` is being called. – Sean Dec 14 '14 at 23:19
  • when i see it calculation.php Method :OPTIONS (canceled) text/plain jquery.min.js:4 0 B 2 ms do you know what is that mean – yoadle Dec 14 '14 at 23:24
  • never seen that before. the only thing i see while googling is this question - http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox I would assume your code is all on the same server, so it is not a `Access-Control-Allow-Origin` issue – Sean Dec 14 '14 at 23:34
  • just wanna ask. Is it happen to your computer or just me? – yoadle Dec 15 '14 at 03:34
  • it is working for me. I have temporary placed it on one of my sites - http://daciusa.org/pella/calc.html – Sean Dec 15 '14 at 04:03
  • That is so weird even i just copy your code and try it it still appear the same problem. I will try to figure out. Any way thank you very much – yoadle Dec 15 '14 at 05:20
  • Are you on a live server? I see that you commented with `file:///C:/xxx/xxx/xxxx/xxx/script.js`. Are you running a `localhost`? if not, your browser does not know what to do with a `.php` file. – Sean Dec 15 '14 at 05:22
  • yea i m using localhost ! but y the browser does not know what to do with a .php file – yoadle Dec 15 '14 at 05:26