SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict bwdow's Avatar
    Join Date
    Feb 2006
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Math with Javascript

    I have a problem with Javascript. I want to make calculation with JS but not avaible. Can you please look to my code whats wrong with it. I want to show all total amount at "toplamtutar" and calculate each of the "tutar" values.
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
    
    <script language="JavaScript">
    function tutartopla()
    {
    var tutar1,tutar2,tutar3,tutar4,tutar5
    t1 = document.form1.tutar1.value
    t2 = document.form1.tutar2.value
    t3 = document.form1.tutar3.value
    t4 = document.form1.tutar4.value
    t5 = document.form1.tutar5.value
    document.form1.toplamtutar.value = eval(t1)+eval(t2)+eval(t3)+eval(t4)+eval(t5)
    }
    </script>
    
    <script language="JavaScript">
    function toplam() {
    var fyt,adt,ttr
    fyt = document.form1.fiyat1.value;
    adt = document.form1.adet1.value;
    ttr1 = fyt1 * adt1;
    document.form1.tutar1.value  = Math.round(ttr1*100)/100;
    document.form1.tutar1.value  = adt;
    }
    </script>
    
    <title>Untitled Document</title>
    </head>
    
    <body>
      <table border="1">
      <tbody><tr>
      <td>group</td>
      <td>product</td>
        <td>price</td>
        <td>quantity</td>
    
        <td>total</td>
      </tr>
      
        <tr>
          <td width="25"><select kt_onchange_attached="true" name="select1" id="select1">
                    <option value="1">Alkolluler</option>
                    <option value="2">Alkolsuzler</option>
                  </select></td>
    
          <td width="30"><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select2" id="select2" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select1">
          <option value="8">Sarap</option><option value="7">BACARDi</option><option value="11">Cin Tonik</option><option value="9">Raki</option><option value="12">Santa Maria</option><option value="10">Votka</option></select></td>
          <td width="7"><input name="fiyat1" type="text" id="fiyat1" value="25" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select2"></td>
          <td width="8">
            <input name="adet1" id="adet1" size="6" onkeydown="toplam();" type="text">      </td>
    
          <td width="10">
            <input name="tutar1" id="tutar1" size="13" value="0" onchange="'tutartopla()" type="text">      </td>
        </tr>
        <tr>
          <td><select kt_onchange_attached="true" name="select3" id="select3">
                    <option value="1">Alkolluler</option>
                    <option value="2">Alkolsuzler</option>
    
                  </select></td>
          <td><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select4" id="select4" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select3">
          <option value="2">KOLA</option><option value="6">KOLA DIET</option><option value="3">SÜT</option><option value="4">Visne Suyu</option></select></td>
          <td><input name="fiyat2" type="text" id="fiyat2" value="12.4" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select4"></td>
          <td><input name="adet2" id="adet2" size="6" type="text"></td>
          <td><input name="tutar2" id="tutar2" size="13" value="0" onchange="'tutartopla()" type="text"></td>
        </tr>
        <tr>
          <td><select kt_onchange_attached="true" name="select5" id="select5">
                    <option value="1">Alkolluler</option>
                    <option value="2">Alkolsuzler</option>
                  </select></td>
          <td><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select6" id="select6" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select5">
          <option value="8">Sarap</option><option value="7">BACARDi</option><option value="11">Cin Tonik</option><option value="9">Raki</option><option value="12">Santa Maria</option><option value="10">Votka</option></select></td>
    
          <td><input name="fiyat3" type="text" id="fiyat3" value="4.8" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select6"></td>
          <td><input name="adet3" id="adet3" size="6" type="text"></td>
          <td><input name="tutar3" id="tutar3" size="13" value="0" onchange="'tutartopla()" type="text"></td>
        </tr>
        <tr>
          <td colspan="4">total</td>
          <td><label>
            <input name="toplamtutar" id="toplamtutar" type="text">
          </label></td>
        </tr>
      </tbody></table>
    
    </body>
    </html>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,174
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    no form

    There is no "form1".

  3. #3
    SitePoint Addict bwdow's Avatar
    Join Date
    Feb 2006
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have modified the code as below but still not working. also function toplam doesn't works too. Do you see a wrong thing in function toplam?

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
    
    <script type="text/javascript">
    function tutartopla()
    {
    var tutar1,tutar2,tutar3,tutar4,tutar5
    t1 = document.form1.tutar1.value
    t2 = document.form1.tutar2.value
    t3 = document.form1.tutar3.value
    t4 = document.form1.tutar4.value
    t5 = document.form1.tutar5.value
    document.form1.toplamtutar.value = Number(t1)+Number(t2)+Number(t3)+Number(t4)+Number(t5)
    }
    </script>
    
    <script type="text/javascript">
    function toplam() {
    var fyt,adt,ttr
    fyt = document.form1.fiyat1.value;
    adt = document.form1.adet1.value;
    ttr1 = fyt1 * adt1;
    document.form1.tutar1.value  = Math.round(ttr1*100)/100;
    document.form1.tutar1.value  = adt;
    }
    </script>
    
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="">
      <table border="1">
        <tbody>
          <tr>
            <td>group</td>
            <td>product</td>
            <td>price</td>
            <td>quantity</td>
            <td>total</td>
          </tr>
          <tr>
            <td width="25"><select kt_onchange_attached="true" name="select1" id="select1">
                <option value="1">Alkolluler</option>
                <option value="2">Alkolsuzler</option>
            </select></td>
            <td width="30"><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select2" id="select2" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select1">
                <option value="8">Sarap</option>
              <option value="7">BACARDi</option>
              <option value="11">Cin Tonik</option>
              <option value="9">Raki</option>
              <option value="12">Santa Maria</option>
              <option value="10">Votka</option>
            </select></td>
            <td width="7"><input name="fiyat1" type="text" id="fiyat1" value="25" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select2" /></td>
            <td width="8"><input name="adet1" id="adet1" size="6" onkeydown="toplam();" type="text" />
            </td>
            <td width="10"><input name="tutar1" id="tutar1" size="13" value="0" onchange="'tutartopla()" type="text" />
            </td>
          </tr>
          <tr>
            <td><select kt_onchange_attached="true" name="select3" id="select3">
                <option value="1">Alkolluler</option>
                <option value="2">Alkolsuzler</option>
            </select></td>
            <td><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select4" id="select4" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select3">
                <option value="2">KOLA</option>
              <option value="6">KOLA DIET</option>
              <option value="3">S&#220;T</option>
              <option value="4">Visne Suyu</option>
            </select></td>
            <td><input name="fiyat2" type="text" id="fiyat2" value="12.4" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select4" /></td>
            <td><input name="adet2" id="adet2" size="6" type="text" /></td>
            <td><input name="tutar2" id="tutar2" size="13" value="0" onchange="'tutartopla()" type="text" /></td>
          </tr>
          <tr>
            <td><select kt_onchange_attached="true" name="select5" id="select5">
                <option value="1">Alkolluler</option>
                <option value="2">Alkolsuzler</option>
            </select></td>
            <td><select kt_onchange_attached="true" wdg:subtype="DependentDropdown" name="select6" id="select6" wdg:type="widget" wdg:recordset="RecSTOK" wdg:displayfield="ADI" wdg:valuefield="KODU" wdg:fkey="GRUP" wdg:triggerobject="select5">
                <option value="8">Sarap</option>
              <option value="7">BACARDi</option>
              <option value="11">Cin Tonik</option>
              <option value="9">Raki</option>
              <option value="12">Santa Maria</option>
              <option value="10">Votka</option>
            </select></td>
            <td><input name="fiyat3" type="text" id="fiyat3" value="4.8" size="6" readonly="readonly" wdg:subtype="N1DependentField" wdg:type="widget" wdg:recordset="RecSTOK" wdg:valuefield="SATISFIYATI" wdg:pkey="KODU" wdg:triggerobject="select6" /></td>
            <td><input name="adet3" id="adet3" size="6" type="text" /></td>
            <td><input name="tutar3" id="tutar3" size="13" value="0" onchange="'tutartopla()" type="text" /></td>
          </tr>
          <tr>
            <td colspan="4">total</td>
            <td><label>
              <input name="toplamtutar" id="toplamtutar" type="text" />
            </label></td>
          </tr>
        </tbody>
      </table>
      </form>
    </body>
    </html>

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,174
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    javascript coding style

    I don't know if it'ds crucial, but I'm in the habit of ending lines (technically statements) with semi-colons. eg. instead of
    var tutar1,tutar2,tutar3,tutar4,tutar5
    var tutar1,tutar2,tutar3,tutar4,tutar5;
    and are you sure you don't want "t1" etc. instead of "tutar1" etc. ?
    I'm also in the habit of using DOM syntax. ie. instead of
    t1 = document.form1.tutar1.value
    t1 = document.getElementById('tutar1').value;
    As the code is now, IE gives an error message
    Line: 93
    Char: 14
    Error: Unterminated string constant
    HTML Code:
    onchange="'tutartopla()" type="text" /></td>
    onchange="'tutartopla()" has an extraneous quote.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Look at the following excerpt. Do you see the problem?

    Code JavaScript:
    fyt = document.form1.fiyat1.value;
    adt = document.form1.adet1.value;
    ttr1 = fyt1 * adt1;


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
  •