Improve company productivity with a Business Account.Sign Up

x
?
Solved

Javascript Help

Posted on 2013-01-28
22
Medium Priority
?
306 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: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 83

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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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 83

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 83

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 83

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

607 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