关于php:我想在更改数量时计算总金额

I want to calculate total amount when I change quantity

我希望在更改数量时获得总金额。

此脚本在while循环中不起作用:(quantity * amount = total_amount)

我该如何解决这个问题? 请帮我解决这个问题。

1
2
3
4
5
6
function calculateTotal() {
   var totalAmt = document.addem.total.value;
   totalR = eval(totalAmt * document.addem.tb1.value);
       
   document.getElementById('total_amount').innerHTML = totalR;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<table>
    <tr>
        <td>Id</td>
        <td>Product</td>
        <td>Quantity</td>
        <td>Total</td>
    </tr>
    <tr>
        <?php
        $sq=mysql_query("select * from cart_sample where email='$ema'");
        while($row = mysql_fetch_array($sq))
        { ?>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['product']; ?></td>

            <form name="addem"  id="addem" />
                <td><input type="text" name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" /></td>
                <input type="hidden" name="total"  value="<?php echo $row['amount']; ?>" />
                <td><span id="total_amount"></span></td>
      <?php } ?>        </tr>
        </table>


由于您正在使用jquery,因此可以避免内联事件并为输入提供常规类,然后为所有输入创建事件:

1
2
3
<input type="text" class='quantity' name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" />
<input type="hidden" class='total' name="total"  value="<?php echo $row['amount']; ?>" />
<span class="total_amount"></span>

JS:

1
2
3
4
5
6
7
$('.quantity').on('input', function(){
    var form = $(this).closest('form');
    var totalAmt = parseInt(form.find('.total').val());
    var quantity = parseInt($(this).val());

    form.find('.total_amount').text(totalAmt*quantity);
})

注意:表单应位于相同的td内。

希望这可以帮助。

1
2
3
4
5
6
7
$('.quantity').on('input', function(){
  var form = $(this).closest('form');
  var totalAmt = parseInt(form.find('.total').val());
  var quantity = parseInt($(this).val());

  form.find('.total_amount').text(totalAmt*quantity);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<form>
  <input type="text" class='quantity' name="tb1" value="1" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">10</span>
</form>

<form>
  <input type="text" class='quantity' name="tb1" value="2" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">20</span>
</form>

<form>
  <input type="text" class='quantity' name="tb1" value="3" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">30</span>
</form>

没有表格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$('.quantity').on('input', function(){
  var parent = $(this).closest('tr');
  var totalAmt = parseInt(parent.find('.total').val());
  var quantity = parseInt($(this).val());

  parent.find('.total_amount').text(totalAmt*quantity);

  calcul_total_quatities();
})

function calcul_total_quatities()
{
  var total = 0;
  $('.total_amount').each(function(){
    total += parseInt( $(this).text() );
  })
  $('.total_all_amounts').text(total);

  post_data_to_server($('.total_amount').val(),total);
}


function post_data_to_server(total_amount,total_all_amounts)
{
  $.ajax({
    type: 'post',
    url: 'your_page.php',
    data: {
      total_amount: total_amount,
      total_all_amounts: total_all_amounts
    },
    success: function( data ) {
      //'data' represent the message back from the PHP page
     
      console.log( data );
    }
  });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<table>
  <tr>
    <td>Id</td>
    <td>Product</td>
    <td>Quantity</td>
    <td>Total</td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' name="tb1" value="1" />
    </td>
    <td><input type="hidden" class='total' name="total" value="10" /></td>
    <td><span class="total_amount">10</span></td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' value="2" />
    </td>
    <td><input type="hidden" class='total' value="10" /></td>
    <td><span class="total_amount">20</span></td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' value="3" />
    </td>
    <td><input type="hidden" class='total' value="10" /></td>
    <td><span class="total_amount">30</span></td>
  </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><span class="total_all_amounts">60</span></td>
  </tr>
</table>


使用jquery只需在页面顶部添加脚本标记即可

1
<script src="https://code.jquery.com/jquery-3.1.1.min.js">

之前的页面底部

1
2
3
4
5
6
7
8
9
10
<script type="text/javascript">

    function calculateTotal() {

        var totalAmount = 0;
        $('#addem').find('input').each(function(){
            totalAmount += parseInt($(this).val()); //convert to integer so sum can be done correctly.
        });
        $('#total_amount').html(totalAmount);  
    }