PDA

View Full Version : Updating Form Rows...


dniwebdesign
09-27-2006, 06:35 AM
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.

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

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

Kor
09-27-2006, 08:39 AM
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)

dniwebdesign
09-27-2006, 08:49 AM
<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.

dniwebdesign
09-27-2006, 08:56 AM
Nevermind, I needed .value on the end of the getElementById()....

Kor
09-27-2006, 08:57 AM
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

dniwebdesign
09-27-2006, 09:02 PM
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.