Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript Help

Posted on 2013-01-28
22
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Meir Rivkin
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 600 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 1400 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 600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

597 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