Web Analytics Made Easy -
StatCounter Updating Form Rows... - CodingForum

Announcement

Collapse
No announcement yet.

Updating Form Rows...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Updating Form Rows...

    Alright, I have 1 form with multiple rows... (http://www.dni-server.no-ip.com/goals.php) for example and source of form.

    Each row has a unique id (info is stored in a mysql database). However I want to update certain areas of the form using javascript (so the user can make sure the info is right, before submitting). The form elements are named according to row.
    Code:
      Average        GAI      PIM
     1_average     1_gai    1_pim
     8_average     8_gai    8_pim
    and the numbers of the form inputs are decided via the database.
    How can I update say the average for each row depending on each element of that row. (When I change 1_pim, it calculates the average and shows it on 1_average, and so on with 8_).
    I figured I could use the below code to change it but no such luck...
    Code:
    <script language="javascript" type="text/javascript">
    function calcStats(id) {
    	alert(document.stats.+id+_average.value);
    }
    </script>
    
    and a form element would have:
    <input type="text" size="5" name="1_pim" onchange="javascript:calcStats(1)">
    The alert was just for testing to see if I could show the value of the 1_average.
    Hope that made sense....
    Last edited by dniwebdesign; Sep 27, 2006, 02:37 AM. Reason: Clarification
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Use JSON

    document.stats[id+'_average'].value

    Take care:
    id is not the same with name (I know is just a function's argument, but I tell you this to take care on the future - when dealing with id, the reference is document.getElementById('element_id') )

    Almost all the native attributes (including value attribute) return a string as their values, thus, if you need a math calculation, you should transform the strings into decimal numbers. A good method, amongs others, is Number()

    var myNumber = Number(document.stats[id+'_average'].value)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Code:
      <script language="javascript" type="text/javascript">
      function calcStats(id) {
      	var myNumber = document.stats[id+'_average'].value;
      	var testNum  = document.getElementById(id+'_average');
      	alert(testNum);
      }
      </script>
      myNumber - alert's an empty alert box
      testNum - alerts "[object]"

      I am still unsure of how to get my value from the certain form inputs.
      Dawson Irvine
      CEO - DNI Web Design
      http://www.dniwebdesign.com

      Comment


      • #4
        Nevermind, I needed .value on the end of the getElementById()....
        Dawson Irvine
        CEO - DNI Web Design
        http://www.dniwebdesign.com

        Comment


        • #5
          use getElementById() only if your element has an id, I tell you once again

          and
          var testNum = document.getElementById('someid');
          alert(testNum);

          will retrurn on object, the object with the id="someid", not a value
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment


          • #6
            Originally posted by Kor View Post
            use getElementById() only if your element has an id, I tell you once again

            and
            var testNum = document.getElementById('someid');
            alert(testNum);

            will retrurn on object, the object with the id="someid", not a value
            That may be but...
            var testNum = document.getElementById('someid').value;
            alert(testNum);

            will alert the value of the object. As well... I updated my form elements to have an id, it works good now. Thanks for you help.
            Dawson Irvine
            CEO - DNI Web Design
            http://www.dniwebdesign.com

            Comment

            Working...
            X