SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Calculating with onChange in form

    Hello!
    I want to crete a form for my invoices. I'm a very newbie in JS, I know just som few things. I have two problem:
    1. I want a functions for calculate a cost with and without tax. I 'm creating this:
    Code:
    <script type="text/javascript">
    // (x) is a number of the php generated row (see the code)
        function FwoTax(x) { 
            woTax+x+.value = eval(quoty+x+.value) * eval(cost+x+.value);
        }
        function FwTax(x) {
            wTax+x+.value = eval(woTax+x+.value)*(eval(tax+x+.value)/100+1);
        }
    </script>
    The result from the brovser is: SyntaxError: Parse error. I'm try it parse with ',", but without result. How can I parse the x variable with ID of the input field?

    2. I'm try the js function without (x) with fixed index woTax1.value (for the 1st row) it worked but not with the Cost with Tax, this one was not be calculated, while I don't change the Tax list.
    PHP Code:
    <form id="form1" name="form1" method="post" action="">
    <?php 
        
    if (!isset ($defRepeat)) { 
            
    $defRepeat 1;
        }
        for (
    $repeat 1$repeat <= $defRepeat$repeat++) {
    ?>
        <p>
        <label>Qty:
        <input name="quoty<? echo $repeat;?>" type="text" id="quoty<? echo $repeat;?>" onchange="FwoTax(<? echo $repeat;?>);" />
        </label>
        <label>Cost/Qty without Tax:
        <input type="text" name="cost<? echo $repeat;?>" id="cost<? echo $repeat;?>" onchange="FwoTax(<? echo $repeat;?>);" />
        </label>
        <label>Tax %:
        <select name="tax<? echo $repeat;?>" id="tax<? echo $repeat;?>" onchange="FwTax();">
            <option value="19">19</option>
            <option value="10">10</option>
            <option value="0">0</option>
        </select>
        </label>
        <label>Cost without Tax:
        <input name="woTax<? echo $repeat;?>" type="text" id="woTax<? echo $repeat;?>" onchange="FwTax();"/>
        </label>
        <label>Cost with Tax:
        <input type="text" name="wTax<? echo $repeat;?>" id="wTax<? echo $repeat;?>" disabled/>
        </label>
        <br />
        <?php
        
    if ($repeat == $defRepeat) {
            echo (
    "<a href=\"?defRepeat=".($defRepeat 1)."\">Add Row</a>");
            if (
    $defRepeat 1) {
                echo (
    "<br /><a href=\"?defRepeat=".($defRepeat 1)."\">Remove Row</a>");
            }
        }
        
    ?>
        </p>
    <?php ?>
    </form>
    Of course on the invoice are much mor input fields. If the Add row link is clicked the page are reloading. For preventing of lose the date I'm use cookies, where each input field is saved to the cookie with onChange event. Is this a good way? Somewhere I read, some browsers have a cookies limatation per domain.

    Thank you very much for any help.

    G.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    Code:
        function FwoTax(x) { 
            var frm = document.getElementById("form1");
            frm["woTax" + x].value = frm["quoty" + x].value * frm["cost" + x].value;
        }
        function FwTax(x) {
            var frm = document.getElementById("form1");
            frm["wTax" + x].value = frm["woTax" + x].value * (frm["tax" + x].value / 100 + 1);
        }

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    Try: ...
    Thanks a much, it's working perfectly.

    G.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •