...

View Full Version : Reading an input array with single element



vkr2007
05-18-2009, 07:23 AM
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).


<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:


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

adios
05-18-2009, 07:32 AM
Why not just check to see if it is a collection? Something like ...


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

vkr2007
05-18-2009, 08:14 AM
Why not just check to see if it is a collection? Something like ...


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

adios
05-18-2009, 08:57 AM
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.

vkr2007
05-18-2009, 11:09 AM
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;

adios
05-18-2009, 11:35 AM
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=2weL0iAfrEMC&pg=PA77&lpg=PA77&dq=javascript+conditional+operator+flanagan&source=bl&ots=_8Zysy-Z9H&sig=p6NvTvF4Dmh-Z1G4FJHGhvUFqr4&hl=en&ei=PSsRStrRCo3ytQPbrpGOAw&sa=X&oi=book_result&ct=result&resnum=1
Array literal: http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=729
Square brackets (must read!): http://www.jibbering.com/faq/faq_notes/square_brackets.html

vkr2007
05-19-2009, 12:21 PM
You're welcome.

This:

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


Hope that's clear.
square_brackets.html[/url]

Many thanks for the explanation.

Raju



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum