48 users online (1 members and 47 guests)  


  Results 1 to 3 of 3

Related

  1. Replies: 0
  2. Replies: 0
  3. Replies: 5
  4. Ignore code and link greeting note code    Forum: HTML Forum
    Replies: 3
  5. Replies: 4
  1. #1
    newphpcoder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Sep 2010
    Posts
    52

    Javascript and php code for autocalculation

    Good day!

    This code is for autocalculate, and I got a problem when i want to add the output of autocalculate in the value of textbox Amount.

    here is the code:
    PHP Code:
    <script type="text/javascript" language="javascript">

    function 
    autocalearn(oText)
    {
    if (
    isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value '';
    }
    var 
    fieldvaloForm oText.formTotEarn 0;
    for (
    aarguments.length; ++a//loop through text elements
    {
    field arguments[a];
    val parseFloat(field.value); //get value
    if (!isNaN(val)) //number?
    {
    TotEarn += val//accumulate
    }
    }
    //oForm.TotEarn.value = TotEarn.toFixed(2); //out
    oForm.TotEarn.value TotEarn.toFixed(2) + document.getElementById('Amount').value;
    }

    </script> 
    the output of this code is when I input 10 and the amount data is 100 the total earn is 10100..its wrong..I want result is 110

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Javascript and php code for autocalculation

    It is because everything entered into an input field is considered a string. When one of the two arguments on either side of the "+" is a string, the two arguments are concatenated. You need to convert the strings to numbers before they will add together.

    Update the last line of code as follows:

    Code:
    oForm.TotEarn.value = parseFloat(TotEarn.toFixed(2)) + parseFloat(document.getElementById('Amount').value);

  3. #3
    newphpcoder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Sep 2010
    Posts
    52

    Re: Javascript and php code for autocalculation

    Good day!

    I resolved that problem, but now I encountered a problem in OVER ALL TOTAL...As I've said I have the total earn and everytime i add earnings in the textboxes like for example in OT Textbox the total earnings textbox change also or should I said what I add it also add in total earnings and same with the total deduction everytime I add deduction it also add in total deduction,, and now I have over all total and the amount of this is the total earn- total deduction and it is autocompute also.but I have problem in total earn, because when I add earnings it did not take effect in over all total, the over all total still the same, but when I add in deductions the overall total was change.. I want it to happen also in total earn.

    here is my new code:

    Code:
    <script type="text/javascript">
    var tot;
    function autocalearn(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, TotEarn = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    {
    field = arguments[a];
    val = parseFloat(field.value); //get value
    if (!isNaN(val)) //number?
    {
    TotEarn += val; //accumulate
    }
    }
    
    tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
    oForm.TotEarn.value = tot.toFixed(2);
    
     
    }
    
    </script>
    
    
    <script type="text/javascript">
    
    function autocalded(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, TotalDed = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    {
    field = arguments[a];
    val = parseFloat(field.value); //get value
    if (!isNaN(val)) //number?
    {
    TotalDed += val; //accumulate
    }
    }
    
    
    var totded=Number(TotalDed) + Number(document.getElementById('Deductions').value);
    oForm.TotalDed.value = totded.toFixed(2);
    var overallTotal = tot - totded;
    
    oForm.TakeHomePay.value = overallTotal.toFixed(2); 
    }
    
    </script>
    Thank you