...

View Full Version : Variable in a variable



shanebanks
03-12-2004, 11:00 PM
<SCRIPT>

function calculate(form)
{
totalcount = form.count.value;
for (i = 1; i < totalcount; i++) {
itemcost = form.a2.value;
form.cost.value = itemcost;
}
}
</SCRIPT>

I am passing data from multiple forms to this script which look like this:
NAME VALUE
a1 10.23
a2 5.34
a3 2.24


I need to add up the values and and display using form.cost.value later in the page. The above example simply displays 5.34.

I believe what I want is itemcost = form.a(value of i here).value, but I am stuck on how to do it. As you can tell I am a javascript newbie.

Shane

shlagish
03-13-2004, 02:49 AM
I don't quite understand, could you post the complete code?

DHTML Kitchen
03-13-2004, 06:10 PM
You can use a hashtable lookup.
var elem = formName.elements.elementName;
var value = elem.value;






function calculate(form) {
var formElements = form.elements;
var totalCost = 0;
var elms = [formElements.a1, formElements.a2, formElements.a3];
for (var i = 0; i < elms.length; i++) {
var value = parseFloat(elms[i].value);
if(isNaN(value)) {
handleNonNumericData(elems[i], value);
return;
}
totalCost += value;
}
form.cost.value = totalCost;
}

function handleNonNumericData(elem, value) {

alert(elem.name + ": "+value + " is not a value");
}



Don't forget to use var.

shanebanks
03-13-2004, 09:21 PM
I should have been more specific. I am pulling the fields from a database and there will be different numbers of rows as data is added. That is why it needs to increment using a variable. I have it working in PHP, but I don't want the screen to refresh when the total is calculated. Here is the code:



<?php require_once('Connections/MySQL.php'); ?>
<?php
mysql_select_db($database_MySQL, $MySQL);
$query_Recordset1 = "SELECT * FROM rates";
$Recordset1 = mysql_query($query_Recordset1, $MySQL) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT>

function calculate(form)
{
totalcount = form.count.value;
totalcost = 0.00;
itemcost = 0.00;
for (i = 1; i < totalcount; i++) {
itemcost = form.a2.value;
//totalcost = 5;
form.cost.value = itemcost; //totalcost;
}
}
</SCRIPT>
</head>

<body>
<p>&nbsp;</p>

<table border="1">
<tr>
<td>RateID</td>
<td>RateDesc</td>
<td>RatePrice</td>
<td>Select</td>
</tr>
<form method="post">

<?php
$count = 0;
do { ?>
<tr>
<td><?php echo $row_Recordset1['RateID']; ?></td>
<td><?php echo $row_Recordset1['RateDesc']; ?></td>
<td><?php echo $row_Recordset1['RatePrice']; ?></td>
<td><input type="checkbox" name="<?php echo "a". $row_Recordset1['RateID']; ?>" value="<?php echo $row_Recordset1['RatePrice']; ?>"></td>

</tr>
<?php $count++; } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

<input type="hidden" name="count" value="<? echo $count; ?>">

<tr><td><input type=button onsubmit="calculate(this.form)" submit value="Calculate"></td></tr>
<tr>
<td valign=top align=right><tt><b>Total: $</b></td>
<td valign=top><input type="text" name="cost" value="" size="5">
</td>
</tr>
</form>
</table>




<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


I have made some changes trying to get it to work. Thanks.

Shane

DHTML Kitchen
03-13-2004, 11:29 PM
This fuction will not do anything meaningful:





function calculate(form)
{
totalcount = form.count.value;
totalcost = 0.00;
itemcost = 0.00;
for (i = 1; i < totalcount; i++) {
itemcost = form.a2.value;
//totalcost = 5;
form.cost.value = itemcost; //totalcost;
}
}



First, think about what you want the function to do. you want it to add the values of some type of input, right? Then name your function something like sumOfInputs()

Now, look at the first variable:
var totalcount = form.count.value;

What is this? Well, lets examine:


alert(typeof totalcount); // "string"
alert(totalcount); // depends on the value in the HTML Form Element.
alert(totalcount.length); // String length of the value in the HTML Form Element.

Why would you want to loop totalcount.length times?

shanebanks
03-14-2004, 12:52 AM
I realize the function does nothing now, what you see is a lot of leftovers from testing various things. I am sure there is a much better way to do this, that is why I need help.

Here is my logic, flawed as it may be:

What the page does is reads a database table of an undefined number of rows (more rows will be added over time) and displays each row with a checkbox next to it. The value of the checkbox becomes the value of RatePrice. The user selects which item(s) to sum and then submits to the function.

I believe the function receives the form name and value for each row. totalcount holds the number of forms that is passed. The purpose of the loop is to increment the form name and add the values. As I learn more it seems that this is the wrong way (or very long way) to acheive the goal of adding all the input from the form, but again I am a newbie.

The onsubmit in this line should be onclick I believe:
<tr><td><input type=button onsubmit="calculate(this.form)" submit value="Calculate"></td></tr>

Maybe I need to totally scrap what I have and start over :confused:

shanebanks
03-14-2004, 01:00 AM
Here is the PHP code that calculates properly. I was hoping to use javascript to display the result on the same page without a refresh.



<?php require_once('Connections/MySQL.php'); ?>
<?php
mysql_select_db($database_MySQL, $MySQL);
$query_Recordset1 = "SELECT * FROM rates";
$Recordset1 = mysql_query($query_Recordset1, $MySQL) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<p>&nbsp;</p>

<table border="1">
<tr>
<td>RateID</td>
<td>RateDesc</td>
<td>RatePrice</td>
<td>Select</td>
</tr>
<form name="select" method="post" action="calculate.php">

<?php
$count = 0;
do { ?>
<tr>
<td><?php echo $row_Recordset1['RateID']; ?></td>
<td><?php echo $row_Recordset1['RateDesc']; ?></td>
<td><?php echo $row_Recordset1['RatePrice']; ?></td>
<td><input type="checkbox" name="<?php echo $row_Recordset1['RateID']; ?>" value="<?php echo $row_Recordset1['RatePrice']; ?>"></td>

</tr>
<?php $count++; } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

<input type="hidden" name="count" value="<? echo $count; ?>"
<tr><td><input type=submit value="Calculate"></form></td></tr>
</table>

<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>



And calculate.php:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$total=0.00;
for ($i=1; $i <= $HTTP_POST_VARS['count']; $i++)
{
$temp= "$i";
$total = $total + $HTTP_POST_VARS[$temp];
}
echo "Total Price: $". $total
?>

</body>
</html>

DHTML Kitchen
03-15-2004, 03:25 AM
Dunno cause I'm a PHP newb, but I think you need a backslash here:



"Total Price: \$". $total





for ($i=1; $i <= $HTTP_POST_VARS['count']; $i++)
{
$temp= "$i";
$total = $total + $HTTP_POST_VARS[$temp];
}
echo "Total Price: $". $total


$HTTP_POST_VARS is a hashtable, or assoc array. In js, you can use an array, Just like I showed you before.


function calculate(form) {
var formElements = form.elements;
var totalCost = 0;
var elms = [formElements.a1, formElements.a2, formElements.a3];
for (var i = 0; i < elms.length; i++) {
var value = parseFloat(elms[i].value);
if(isNaN(value)) {
handleNonNumericData(elems[i], value);
return;
}
totalCost += value;
}
form.cost.value = totalCost;
}

function handleNonNumericData(elem, value) {

alert(elem.name + ": "+value + " is not a value");
}


Think of elms as the same thing as $HTTP_POST_VARS, except instead of an assoc array, it's just an array with numeric indices.




var elms = [formElements.a1, formElements.a2, formElements.a3];

Think of elms as the same thing as $HTTP_POST_VARS, except instead of an assoc array, it's just an array with numeric indices.

shanebanks
03-15-2004, 06:07 PM
Sorry, but I still don't understand this line:

var elms = [formElements.a1, formElements.a2, formElements.a3];

Since there may be 3 forms submitted or 10 or 15 would I need to have a formElements.?? for each form and therefore if rows were added to the database I would need to edit this line? I am probably missing something. You should be able to see what I am working on here (http://65.68.22.12) . Thanks for your help so far.

SB

Unit
03-15-2004, 08:02 PM
This function should sum the checkboxes up as long as the rate id's look like a1, a2, a3, etc...



function calculate(form)
{
var formElements = form.elements;
var totalCost = 0;
for (var i = 0; i < form.count.value; i++)
{
var frmElement = formElements["a"+i];
if (frmElement.checked == true)
{
totalCost += parseFloat(frmElement.value);
}
}
form.cost.value = totalCost;
}

shanebanks
03-15-2004, 08:40 PM
Thanks for the response, I tried the code and I get an error when it calculates.

See here (http://65.68.22.12)

shanebanks
03-15-2004, 08:45 PM
Never mind the last post. I changed the line:
for (var i = 0; i < form.count.value; i++)

to

for (var i = 1; i <= form.count.value; i++)

and it seems to be working properly. Thanks!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum