0

Hi everyone Im new here and not very good with Javascript, so here im asking for a little help for my proyect

the problem is Im trying to calculate text fields from results of php query

my fields come from the query as amount1, amount2....

what i need is for JS to Sum those fields dynamically, I already did code for when i have all the values and for when the text field name is the same, but never for query results where the names are different

this is what i have so far but its not working

function calculatesumexcon() {
  
  var a, total = 0;
  var countJS = "<?=$count?>"; <-- variable of php that gives me the number of rows im getting on the query 
  var i ;
  
  for ($i=0;$i<countJS;$i++){
  var elements = document.getElementsById('amount' + i);

   for(a=0; a<elements.length; a++){
          total += (parseFloat(elements[a].value));
   }
}  
   document.getElementById("totalextracon").value = total ;
   
}

Please help!

Edited by madehoney: n/a

3
Contributors
9
Replies
13
Views
6 Years
Discussion Span
Last Post by piepie22
0

Why don't you do this is PHP? You can sum them as you query.

Yes I know there is a way to get this from a query but you require to use a button for it... in this case we are on the page where the user can modify the quantity if they wish from the database, So I show the data, they modify and see the result for the sum before submitting the form

0

I can't really visualize how your application works.

On the page there is a textbox, user input and it calculates the value of this textbox + some value from the DB?

If this is the case, you can sum the amount from query first. Then onchange textbox value run a function that simply adds up the 2 values and output in a div.

Edited by Javvy: n/a

0

I can't really visualize how your application works.

On the page there is a textbox, user input and it calculates the value of this textbox + some value from the DB?

If this is the case, you can sum the amount from query first. Then onchange textbox value run a function that simply adds up the 2 values and output in a div.

this is a consult page for service cost...

what the user do is search for services costs in a determinate month & year

the result will be a drop down list (populated by data from DB) & the textbox with the cost of the service (also saved on the db)

this show as many times as services there is for that month & year hence the amount1, amount2, amount3, & so on

The user will modify cost of service if he wishes & has to see the amount changing on another textbox totalextracon, because that is part of another math calculation. I dont care to do the SUM to show when i do the query because that field already has a value, (It already do the SUM when you register for the first time so its already on DB, I just show it)

So i need to this with JavaScript because it has to be modified (dynamically) at the moment, its required by the costumer, as i said before I did it on another cases but this i really dont know how to put together the changing of the name

the help will be appreciated

0

I'll try to help slowly..

Let's start from this..

<script>
function showcost() {
	document.getElementById("cost").value = document.getElementById('serviceslist').value;
}
</script>

<?php
$services = array(
"0" => array("desc" => "Service 1", "cost" => "$100"),
"1" => array("desc" => "Service 2", "cost" => "$200"),
"2" => array("desc" => "Service 3", "cost" => "$300"),
);
?>

<select id="serviceslist" onchange="showcost()">
<?php
for ($i=0;$i<count($services);$i++) {
	echo "<option value='".$services[$i]['cost']."'>".$services[$i]['desc']."</option>";
}
?>
</select>

<input type="text" id="cost"">

I've used a array to emulate your DB..

Edited by Javvy: n/a

0

Hi, It seems that code is some sort of query where you add the values to the select, & that is not what I need, all my query & updates are done, I just need a math calculation

I will put here this picture where you can see what i have already & I hope you can understand what Im asking...

http://i226.photobucket.com/albums/dd197/Madehoney/sample.jpg

Edited by madehoney: n/a

0

Hey,

You can try this. Basically the concept should be there but you need to populate your own DB values into more dropdown values.

<script>
function showcost() {
	document.getElementById("cost").value = document.getElementById('serviceslist').value;
}

function sum() {
	var x = document.getElementById('cost').value;
	var y = document.getElementById('amount').value;
	var total = parseFloat(x) + parseFloat(y);
	document.getElementById("total").value = total;
}
</script>

<?php
$services = array(
"0" => array("desc" => "Service 1", "cost" => "100"),
"1" => array("desc" => "Service 2", "cost" => "200"),
"2" => array("desc" => "Service 3", "cost" => "300"),
);
?>

<select id="serviceslist" onchange="showcost(); sum()">
<?php
for ($i=0;$i<count($services);$i++) {
	echo "<option value='".$services[$i]['cost']."'>".$services[$i]['desc']."</option>";
}
?>
</select>

<input type="text" id="cost"" onchange="sum()" value="0">

<input type="text" id="amount" onchange="sum()" value="0">

<input type="text" id="total" value="0">

Edited by Javvy: n/a

0

Hi a friend helped me to do it was very similar to the one I posted on the first post... thank you for the effort anyway

I was wondering (i dont know if I need to open another thread for this, let me know please)

I have 2 drop downs (2nd is hidden) populated from database, both should have on the start the same choices BUT if I make 1 choice on the first drop down, that selected option needs to be deleted from drop down 2, and so on if i keed adding drop downs...

there is a way to do this on Javascript?

0

Can you help to post your solution your friend gave?
as I am working on something similar as well,
any help would be appreciated.
Thanks.

Hi a friend helped me to do it was very similar to the one I posted on the first post... thank you for the effort anyway

I was wondering (i dont know if I need to open another thread for this, let me know please)

I have 2 drop downs (2nd is hidden) populated from database, both should have on the start the same choices BUT if I make 1 choice on the first drop down, that selected option needs to be deleted from drop down 2, and so on if i keed adding drop downs...

there is a way to do this on Javascript?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.