1

I have the following JavaScript code which is writing in a HTML form:

  <script type="text/javascript">
    function updatesum() {
    document.functie.column8.value = (document.functie.column6.value -0) * (document.functie.column7.value -0);
    document.functie.column12.value = (document.functie.column10.value -0) * (document.functie.column11.value -0);
    document.functie.column16.value = (document.functie.column14.value -0) * (document.functie.column15.value -0);
    document.functie.column20.value = (document.functie.column18.value -0) * (document.functie.column19.value -0);
   }

and with the php form like:

echo "<td><input name=\"column6\" onChange=\"updatesum()\"></td>";
echo "<td><input name=\"column7\" onChange=\"updatesum()\"></td>";
  1. The problem is that when I put some values in the first form, it gives me the sum, but if I don't put some values it gives me zero, which I do not want:

    http://img17.imageshack.us/img17/5456/hyhu.png http://img17.imageshack.us/img17/5456/hyhu.png

  2. In the bottom I have a sum of all the values from the 3rd column, but the it gives me numbers like 2400 or 2300.44, and I want an output of 2,400 or to roundup the 2300.44 to 2,300. How can I do that?

Leon
  • 51
  • 8
  • You should ask only one question. *"it gives me zero, which I do not want"* What do you want instead? *"I want an output of 2,400 or to roundup the 2300.44 to 2,300"* See [How to format numbers using javascript?](http://stackoverflow.com/q/5731193/218196) and [How can I round down a number in Javascript?](http://stackoverflow.com/q/1435975/218196). – Felix Kling Jul 20 '13 at 18:19
  • "*if I don't put some values it gives me zero, which I do not want*"...what do you want then? – Shawn31313 Jul 20 '13 at 18:23
  • Sorry Felix Kling and Shawn31313, are two questions ... at the question one: i do not want any character to be seen, because that character (of zero in my case), is submitted in the mysql database. – Leon Jul 20 '13 at 18:36

1 Answers1

1

1/ You're multiplying with a value '' automatically cast to an integer, so 0. This is normal that you end up with 0. If you do not want that, you'll have to create a function that check the value of the inputs and return '' if one of them is '' something like:

function multiply(v1, v2) {
    if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
        return '';
    } 
    return v1*v2;
}

2/ You'll need to add a format function, that will format your code and add the , where it must and get ride of the decimals (or use links in that comment javascript updatesum() problems) :

function formatNumber(num) {
    var formatted = '';
    num = Math.ceil(num) + '';
    for (var i = num.length - 1, j = 0; c = num.charAt(i); i--) {
        if (j % 3 === 0) {
            c = c + ',';
        }
        formatted = c + formatted;
        j++;
    }
    return formatted.trim();
}

You asked for an example. This should do it in place of your current updatesum function:

function multiply(v1, v2) {
    if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
        return '';
    } 
    return v1*v2;
}
function multiplyAndFormat(v1, v2) {
    return formatNumber(multiply(v1, v2));
}

function formatNumber(num) {
    var formatted = '';
    num = Math.ceil(num) + '';
    for (var i = num.length - 1, j = 0; c = num.charAt(i); i--) {
        if (j % 3 === 0) {
            c = c + ',';
        }
        formatted = c + formatted;
        j++;
    }
    return formatted.trim();
}

function updatesum() {
    document.functie.column8.value = multiplyAndFormat(document.functie.column6.value, document.functie.column7.value);
    document.functie.column12.value = multiplyAndFormat(document.functie.column10.value, document.functie.column11.value);
    document.functie.column16.value = multiplyAndFormat(document.functie.column14.value, document.functie.column15.value);
    document.functie.column20.value = multiplyAndFormat(document.functie.column18.value, document.functie.column19.value);
}

should do it

Community
  • 1
  • 1
dievardump
  • 2,484
  • 17
  • 21
  • dievardump: Could you provide please an example using my code on my function?..I've tried but without success :(. Problem is that i could have only one function in the php field, and in the javascript i must check for the php like document.functie.column"number".value, so your example it might be functional, but for a newbie in javascript i do not understand how to modify it to work. Thank you. – Leon Jul 22 '13 at 13:41
  • and now in the php code i must have multiplyAndFormat(), and the updatesum is needed no more? – Leon Jul 22 '13 at 15:52
  • I updated my post again. And I want to say that if you can not understand that code and what to do with it, you should stop going further and you should go back to basics. We are not here to do your code but to help you. I am sorry but I will not write more code about that subject except to correct mistakes if there is. – dievardump Jul 22 '13 at 15:56
  • your code it's working 99%. practically if (j % 3 === 0) it gives me always a format like "0," or "455,", changed to (j % 3 === 1), and it's working to the thousands like 1,000 or 34,000 but if i get some decimals it gives me 1,000,00 which is not correct and it must be 1,000.00 ... dievardump: sorry, but i admit it's difficult for me to understand the javascript, and in my project i needed only to do me some real-time math. – Leon Jul 22 '13 at 18:09
  • 1
    j !== 0 && j%3 === 0 should do it. – dievardump Jul 23 '13 at 09:05
  • when i come in London i give you a beer :), thank you for all the help, you save me some precious time. – Leon Jul 23 '13 at 11:04