Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading an input array with single element

    I am trying to develop a small web based inventory programme. In the programme, after orders are placed for supply of an item, the materials are to be recieved by the stores. For this a "goods received page" is used. On start the page loads items as per the purchase order in the database. Javascript is used to calculate the cost of each item and total cost of all items. since the number of items vary from order to order I use the following code to load the page. ( PHP is used in the server side).
    Code:
    <input type='text' size='10' name ='rqty[]' value='1.000'  align='right' onchange="recalculate()">
    <input type='text' size='10' name ='rqty[]' value='2.000'  align='right' onchange="recalculate()">
    <input type='text' size='10' name ='rqty[]' value='3.000'  align='right' onchange="recalculate()">
    recalculate() is a javascript function used recalculate the totals whenever there is a change in any of the inputs.
    in the javascript the following code is used to read the values:
    Code:
     var qty = document.form1.elements["rqty[]"];
     var tax = document.form1.elements["rtax[]"];
     var cost = document.form1.elements["rcost[]"];
     var rsize =qty.length;
      for(i=0; i < rsize; i++)
    		{
    		 var rate1 = rate[i].value;
    		 var qty1 = qty[i].value;
    		 var tax1 = tax[i].value;
    		 var cost1 = rate1*qty1;
                         }
    My problem is that the above will work whenever there are two or more items and the variable qty is actually an array. But it does not work when there is only one item. In other words, it does not recognises a single element array. value of rsize is returned as undefined.
    Splitting the loading process with seperate naming for single items as rqty and multiple items as rqty[] is not possible as option to be given to delete items from a multiple item page. This may end up in a single item page eve n when there are multiple items at load time.

    Any quick help will be highly appreciated.

    Raju

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Why not just check to see if it is a collection? Something like ...

    Code:
    var qty = document.form1.elements["rqty[]"];
     var tax = document.form1.elements["rtax[]"];
     var cost = document.form1.elements["rcost[]"];
     var rsize =qty.length;
     if (rsize)
     {  //branch
      for(i=0; i < rsize; i++)
    		{
    		 var rate1 = rate[i].value;
    		 var qty1 = qty[i].value;
                     }
     }
     else 
     { //treat it like a single element

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by adios View Post
    Why not just check to see if it is a collection? Something like ...

    Code:
    var qty = document.form1.elements["rqty[]"];
     var tax = document.form1.elements["rtax[]"];
     var cost = document.form1.elements["rcost[]"];
     var rsize =qty.length;
     if (rsize)
     {  //branch
    
     }
     else 
     { //treat it like a single element
    Thanks for the reply.

    How can I access the single element ?
    I used the follwing methods but without success:
    document.form1.rqty.value
    document.getElementsBy Name("rqty").value
    document.getElementsBy Name("rqty[]").value

    the error messeages are either " Cannot convert undefined or null to Object" or "undefined".

    if I change the name of the input from "rqty[]" to "rqty" it works. But I cannot do it for reasons stated in the original post.

    reagrds,
    Raju

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    function recalculate()
    {
       var qty = document.form1.elements["rqty[]"];
       var rate = document.form1.elements["rrate[]"];
       var tax = document.form1.elements["rtax[]"];
       var cost = document.form1.elements["rcost[]"];
       var notColl = ('undefined' == typeof qty.length);
       qty = (notColl) ? [qty] : qty;
       rate = (notColl) ? [rate] : rate;
       tax = (notColl) ? [tax] : tax;
       cost = (notColl) ? [cost] : cost;
       var rsize = qty.length;
       for(i = 0; i < rsize; i++)
       {
          var qty1 = qty[i].value;
          var rate1 = rate[i].value;
          var tax1 = tax[i].value;
          var cost1 = rate1 * qty1;
    Just guessing at some of your form. I assume your php echoes one each of these fields as a group. If there's only one of a group, put each field in an array & loop just the same.

    If that doesn't help, you'll need to post a complete sample that isn't working.

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by adios View Post
    Code:
    function recalculate()
    {
       var qty = document.form1.elements["rqty[]"];
       var rate = document.form1.elements["rrate[]"];
       var tax = document.form1.elements["rtax[]"];
       var cost = document.form1.elements["rcost[]"];
       var notColl = ('undefined' == typeof qty.length);
       qty = (notColl) ? [qty] : qty;
       rate = (notColl) ? [rate] : rate;
       tax = (notColl) ? [tax] : tax;
       cost = (notColl) ? [cost] : cost;
       var rsize = qty.length;
       for(i = 0; i < rsize; i++)
       {
          var qty1 = qty[i].value;
          var rate1 = rate[i].value;
          var tax1 = tax[i].value;
          var cost1 = rate1 * qty1;
    Just guessing at some of your form. I assume your php echoes one each of these fields as a group. If there's only one of a group, put each field in an array & loop just the same.

    If that doesn't help, you'll need to post a complete sample that isn't working.
    WOW! This work excellently. Many thanks to you.
    By the way, can you explain what is mean by this line, particulary that of [qty]. why [qty] is used instead of qty[] for the array ?

    qty = (notColl) ? [qty] : qty;

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    You're welcome.

    This:

    <input ....... name ='rqty[]'

    ... doesn't do anything in the browser. Unlike php, which creates an array, to the browser it's just a name - with two illegal characters. It fails in dot syntax:

    document.form1.elements.rqty[]

    ... as those are identifiers (variable names); they're part of the language, and square brackets are operators signifying 'array.' Perfectly OK to use them hash-style though (actually not, but don't tell 45,000 php programmers that). This:

    document.form1.elements['rqty[]']

    ... works fine. But there's a wrinkle, which has nothing to do with the brackets: if you have more than one element in a form with this or any of the same name, examining the above reference will reveal an array (nodelist) of all the same-named elements. So you need to loop. You can branch, and loop when there's a collection and not when there isn't, but I prefer to just plunk those singlets in an array (literal) and 'loop' once.


    var notColl = ('undefined' == typeof qty.length); //Boolean, true if not collection/ no 'length' property
    qty = (notColl) ? [qty] : qty; // so: put it in an array


    Hope that's clear.

    Ternary operator: http://books.google.com/books?id=2we...esult&resnum=1
    Array literal: http://www.devhood.com/Tutorials/tut...utorial_id=729
    Square brackets (must read!): http://www.jibbering.com/faq/faq_not..._brackets.html
    Last edited by adios; 05-18-2009 at 12:23 PM.

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by adios View Post
    You're welcome.

    This:

    <input ....... name ='rqty[]'


    Hope that's clear.
    square_brackets.html[/url]
    Many thanks for the explanation.

    Raju


  •  

    Posting Permissions

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