Solved

Javascript Help

Posted on 2013-01-28
22
261 Views
Last Modified: 2013-01-28
Hey,

I'm trying to loop through fields that have number appended to them in php and calculate a running total in each total field.  This is the code I have so far:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById(numberNights[i]).value;
var rate = document.getElementById(rate[i]).value
var tax = document.getElementById(taxAmount[i]).value
var total = document.getElementById(totalCost[i]).value
var subtotal = parseFloat(document.getElementById(rate[i]).value) + parseFloat(document.getElementById(taxAmount[i]).value) ;
var total = subtotal * parseFloat(document.getElementById(numberNights[i]).value);
document.getElementById(totalCost[i]).value(total.toFixed(2));
	}
}

I keep getting "numberNights[i] is not defined",  I admittedly suck at javascript.

Open in new window

0
Comment
Question by:nickinthooz
  • 11
  • 6
  • 4
  • +1
22 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827212
possibly you need:
var numnights = document.getElementById('numberNights'+i).value;

Open in new window

(and for the others as well)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38827217
i guess u meant that:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById('numberNights'+i).value;
var rate = document.getElementById('rate'+i).value
var tax = document.getElementById('taxAmount'+i).value
var total = document.getElementById('totalCost'+i).value
var subtotal = parseFloat(document.getElementById('rate'+i).value) + parseFloat(document.getElementById('taxAmount'+i).value) ;
var total = subtotal * parseFloat(document.getElementById('numberNights'+i).value);
document.getElementById('totalCost'+i).value(total.toFixed(2));
	}
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38827221
try this :
function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById("numberNights"+i).value;
var rate = document.getElementById(rate+i).value
var tax = document.getElementById(taxAmount+i).value
var total = document.getElementById(totalCost+i).value
var subtotal = parseFloat(document.getElementById(rate+i).value) + parseFloat(document.getElementById(taxAmount+i).value) ;
var total = subtotal * parseFloat(document.getElementById(numberNights+i).value);
document.getElementById(totalCost+i).value =total.toFixed(2);
	}
}

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827223
Indeed. But it would be nice to see some html to make sure this will work.
0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827231
Thanks for the quick responses,  I'm still catching this error:

TypeError: document.getElementById(...) is null
[Break On This Error] 	

var numnights = document.getElementById("numberNights"+i).value;

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827234
maybe it's:
var numnights = document.getElementById('numberNights['+i+']').value; 

Open in new window

0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827239
as requested:

		<tr id="tableRow<?php echo $cnt; ?>" class="tableRow<?php echo $cnt; ?>">
										<td>  <button type="button" id="deleteButton<?php echo $cnt; ?>" name="deleteButton<?php echo $cnt; ?>" class="btn pull-right" <?php echo $disabled; ?> onclick="deleteRow(this);" /><li class="icon-trash"></li></button></td>
										<td><input type="text" name="roomNumber<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?> value="<?php echo $stays->roomNumber; ?>"></td>
										<td><input type="text" name="name<?php echo $cnt; ?>" id="name<?php echo $cnt; ?>" class="validate[required] text-input span2" <?php echo $read_only; ?>   value="<?php echo $stays->name; ?>"></td>
										<td><input type="text" onblur="check_id(this.value, <?php echo $cnt; ?>);" name="employeeId<?php echo $cnt; ?>" id="employeeId<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->employeeId; ?>"></td>
										<td><input type="text" name="costCenter<?php echo $cnt; ?>" id="costCenter<?php echo $cnt; ?>" class="validate[required] text-input span1" <?php echo $read_only; ?>   value="<?php echo $stays->costCenter; ?>"></td>						
										<td><input type="text" style="width:45px;" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" name="afeNumber<?php echo $cnt; ?>"  <?php echo $read_only; ?>   value="<?php echo $stays->afeNumber; ?>"></td>						
										<td><input type="text" name="dateIn<?php echo $cnt; ?>" id="dateIn<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] spanDate" <?php echo $read_only; ?>   value="<?php echo $dateIn; ?>"></td>
										<td><input type="text" name="dateOut<?php echo $cnt; ?>"  id="dateOut<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] spanDate" <?php echo $read_only; ?>   value="<?php echo $dateOut; ?>"></td>
										<td><input type="text" onchange="calculate(this)" name="numberNights<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->numberNights; ?>"></td>
										<td><input type="text" name="folioNumber<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1"  <?php echo $read_only; ?> value="<?php echo $stays->folioNumber; ?>"></td>
										<td><input type="text" onchange="calculate(this.form)" name="rate<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>  value="<?php echo $stays->rate; ?>"></td>
										<td><input type="text" onchange="calculate(this.form)" name="taxAmount<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>  value="<?php echo $stays->taxAmount; ?>"></td>
										<td><input type="text" name="totalCost<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->totalCost; ?>"></td>
									  
									</tr>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38827248
getElementById => get Element By Id

do you have something like this in your page :
<input type="text" name="THIS_IS_A_NAME" id="numberNights1" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights2" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights3" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights4" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights5" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights6" />
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827251
Or use the form to access elements, something like:
var numnights = document.form['yourFormName'].elements['numberNights'+i].value;

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38827261
rigth click on your page in your web browser, choose view source and post it here
thanks
0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827262
I think it may be the id's, I think I may be on crack...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827276
;-)

Some inputs have an id, but not all. Either correct that or try my suggestion regarding document.form
0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827288
That fixed the errors, now I'm getting no errors, but I'm not getting any results either.  Updated Code:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementsByName('numberNights'+i).value;
var rate = document.getElementsByName('rate'+i).value
var tax = document.getElementsByName('taxAmount'+i).value
var total = document.getElementsByName('totalCost'+i).value
var subtotal = parseFloat(document.getElementsByName('rate'+i).value) + parseFloat(document.getElementsByName('taxAmount'+i).value) ;
var total = subtotal * parseFloat(document.getElementsByName('numberNights'+i).value);
document.getElementsByName('totalCost'+i).value = (total.toFixed(2));
	}
}

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827312
That's not what I suggested. It probably doesn't work because GetElementsByName returns an array of elements. Maybe you can fix it by taking the first element with [0] and then take the value but it's not a very nice solution.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827323
What does your form tag look like?
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 150 total points
ID: 38827334
scratch that question, it's in the function argument. try this:
function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = form.elements['numberNights'+i].value;
var rate = form.elements['rate'+i].value;
var tax = form.elements['taxAmount'+i].value;
var total = form.elements['totalCost'+i].value;
var subtotal = parseFloat(rate) + parseFloat(tax);
var total = subtotal * parseFloat(numnights);
form.elements['totalCost'+i].value =total.toFixed(2);
	}
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 38827336
getElementsByName => get Elements by name return an array because you can have more than one element having the same name !

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementsByName('numberNights')[i].value;
var rate = document.getElementsByName('rate')[i].value
var tax = document.getElementsByName('taxAmount')[i].value
var total = document.getElementsByName('totalCost')[i].value
var subtotal = parseFloat(document.getElementsByName('rate')[i].value) + parseFloat(document.getElementsByName('taxAmount')[i].value) ;
var total = subtotal * parseFloat(document.getElementsByName('numberNights')[i].value);
document.getElementsByName('totalCost')[i].value = (total.toFixed(2));
	}
}

Open in new window


...but was simpler to add an ID attribute to ALL your fields..
0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827343
<?php 
				$attributes = array( 'id' => 'myform');

				echo form_open_multipart('voucher/update_voucher', $attributes);
				?>

Open in new window


produces <form action="http://vouchers.........net/index.php/voucher/update_voucher" method="post" accept-charset="utf-8" id="myform" enctype="multipart/form-data">
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 150 total points
ID: 38827351
one problem left in the html:
<input type="text" onchange="calculate(this)" name="numberNights

Open in new window

should be:
<input type="text" onchange="calculate(this.form)" name="numberNights

Open in new window

The others are already correct.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827369
@leakim971: yep, that's another way to solve it but the php code needs to be amended in that case as well because at the moment the counter is in the element name.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38827376
Nick, I see you posted your form tag but in the mean time I found it wasn't necessary.
0
 
LVL 6

Author Comment

by:nickinthooz
ID: 38827379
I went through and just added Id's to the fields I needed to use and it worked perfectly.  Thanks guys. Oversight on my part.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
updating the date data 12 25
Randomize in Owl Carousel v1.3.2 6 21
two layers of simple javascript validation 2 28
Grunt Copy file to another destination. 1 17
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now