Now, a Javascript and ASP question...

Results 1 to 2 of 2

Thread: Now, a Javascript and ASP question...

  1. #1
    Join Date
    Dec 1969

    Default Now, a Javascript and ASP question...

    Hi again,<BR><BR>I have another problem now.<BR><BR>I have several input type text fields which have the name property dinamically generated from the database and have the value property also generated from the database (they are numbers with decimals).<BR><BR>And I have another input type text field which has a fixed name and fixed value (again, number with decimals).<BR><BR>And the last input type text field with fixed name which has the value the result of all fields (dinamic and fixed) - the calculation is done here.<BR><BR>When a user loads first time the page all values are taken from the database and displayed (the total is done using ASP).<BR><BR>All these fields values can be changed by the user after fist time loading.<BR><BR>Now after the page has loaded first time maybe the user wants to change a value in one or more fields.<BR><BR>I want that the total to be calculated in real-time (wihtout using any button).<BR><BR>With Javascript it can be done.<BR><BR>I have this script in the head of the ASP page:<BR>------------<BR>&#060;script language="Javascript"&#062;<BR>functiont the_amount() {<BR> <BR>document.invoice.result.value=document.invoice .price.value+document.invoice.shipping.value;<BR>} <BR>&#060;/script&#062;<BR>----------<BR><BR>These fields are in a loop:<BR>----------<BR>&#039; dynamic generated name for this field - which will be more after loop<BR>&#060;input type="text" name="price_&#060;%=rsorder("productid")%&#062;" class=input_price value="&#060;%= formatnumber(rsorder("priceperunit"))%&#062;" onChange="the_amount()"&#062;<BR><BR>&#039; fixed name, fixed value<BR>&#060;input type="text" name="shipping" class=input_price value="3,90" onChange="suma()"&#062;<BR><BR>&#039; fixed name, dynamic value - depends on the value of the fields above<BR>&#060;input type="text" name="result" class=input_price value="&#060;% =formatnumber(subtotal) %&#062;"&#062;<BR><BR>----------<BR><BR>The problem is that in the head I do not have "document.invoice.price.value" dynamically generated.<BR><BR>How can I achieve that?<BR><BR>The SQL I&#039;m using is:<BR>-------<BR>set rsorder=db.execute("SELECT * FROM orderitems INNER JOIN (customers INNER JOIN orders ON orders.custid=customers.custid) ON orders.orderid=orderitems.orderid where orders.orderid=" & id)<BR>-------<BR><BR>And is placed before the head of the page but the loop is after the head of the page (in the body somewhere).<BR><BR>Thanks for any suggestion and sorry for my long post.<BR><BR>Mircea

  2. #2
    Join Date
    Dec 1969

    Default Big mistake...

    *ALL* the fields you generate in that loop should get dynamic names! PERIOD!<BR><BR>Like this:<BR><BR>&#060;input name="price_&#060;%=rsorder("productid")%&#062;" <BR> value="&#060;%= formatnumber(rsorder("priceperunit"))%&#062;" onChange="recalc(this);"&#062;<BR><BR>&#060;input name="shipping_&#060;%=rsorder("productid")%&#062; " <BR> value="3,90" onChange="recalc(this);"&#062;<BR><BR>&#060;input name="result_&#060;%=rsorder("productid")%&#062;" <BR> value="&#060;% =formatnumber(subtotal) %&#062;" onChange="recalc(this);"&#062;<BR><BR>[I put the recalc even on the "result_XXX" field so that if the user tried to change it the value would be reset right back to the correct value!]<BR><BR>And then the JS code:<BR><BR>&#060;SCRIPT&#062;<BR>function recalc( fld )<BR>{<BR> var fldname =; // get name of field<BR> var underline = fldname.indexOf( "_" ); // where is underline?<BR> var productid = fldname.substr( underline + 1 ); // all chars after _<BR><BR> var frm = fld.form; // get the form for all 3 fields<BR> <BR> var price = frm.elements["price_" + productid];<BR> var shipping = frm.elements["shipping_" + productid];<BR> var result = frm.elements["result_" + productid];<BR><BR> result.value = parseFloat( price.value ) + parseFloat( shipping.value );<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>NOTE: That will *NOT* display the result as a "formatted" number. That 3,90 will probably end up appearing as 3,9 or maybe 3.9 (but certainly without the trailing zero).<BR><BR>To format the result as "money", you need a separate format routine, which JavaScript does *not* have built in.<BR><BR><BR><BR><BR>----------<BR>

Posting Permissions

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