hello daniweb friends
i need help in my below calculation script all go right but just in the filed shippingtotal it display NaN i don't know why
i tried the comment in line 27 and delete line 6 but still same. HELP PLEASE

<script type="text/javascript">
function calculatePtotal() {
    var totalofptotal = 0;
    var wholeqtty = 0; 
    var totalofweight = 0; 
    var shippingtotal = 0;
    $("tr.calc", "#myTable", "#panierform").each(function (i, row) {
        $row = $(row);
        var value = parseFloat($('.prix', $row).val());
        var quantity = parseFloat($(".quantity", $row).val());
        var poid = parseInt($(".poid", $row).val());
        var shipping = parseFloat($(".shipping", $row).val());
        var ptotal = (value * quantity).toFixed(2);
        var weight = (poid * quantity) .toFixed(0);
        $(".quantiy", $row).val(quantity);
        $(".ptotal", $row).val(ptotal);
        $(".weight", $row).val(weight);
        totalofptotal += parseFloat(ptotal);
        wholeqtty += parseInt(quantity);
        totalofweight += parseInt(weight);
    });
    $("#wholeqtty") .val(wholeqtty.toFixed(0));
    $("#totalofweight") .val(totalofweight.toFixed(0));
    $("#totalofptotal").val(totalofptotal.toFixed(2));
}
{
    shippingtotal =(shipping * totalofweight).toFixed(2) ; /* i tried to put he var shippingtotal =(shipping * totalofweight).toFixed(2) ; and delete line  */
}
{
    $("#shippingtotal").val(shippingtotal);
}
calculatePtotal();
$(".quantity, .shipping").change(function () {
    calculatePtotal();
});
</script>

Recommended Answers

All 6 Replies

U must remember that parseFLoat convert a string to a floating point number any thing else and it will return NaN [more on parseFloat ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) at line 18 of your code u are trying to convert to float a value that is already a float so remove the parseFloat at line 18 and it should work Also on line 20 u are doing the same thing with parseInt. Remove it and it will work

line 18 is (a price) floating point number stocked in DB as float
and line 20 is total of weight and weight is stock in db as integer.
and the result of line 18 and 20 work fine on line 23 and 24,
if i change i will have trouble i think.

if i shoud edit the title i would say how to sum to value of diferent type i tried to change line 18 and 20 but nothing happened and here

shipping mut be Float as it is var shipping = parseFloat($(".shipping", $row).val());cause the value in field is

<select name="Shipping" class="shipping" id="shipping"> Shipping
        <option  value="0" selected="selected">0.00</option>
           <option value="0.0068">0.0068</option>
           <option value="0.026">0.026</option>
           <option value="0.03076">0.3076</option>
         </select>

And the same for total of weight too it a integer var weight = (poid * quantity) .toFixed(0);//calculate row weight
and total fo wight totalofweight += parseFloat(weight);

{
    shippingtotal =(shipping * totalofweight).toFixed(2) ; /* i tried to put he var shippingtotal =(shipping * totalofweight).toFixed(2) ; and delete line  */
}

Hello friend
here si the HTML FORM

<form name="panierform" id="panierform" method="post" action="">
      <table id="myTable" width="80%" border="2">
        <?php do { ?>
          <tr class="calc">
            <td><label for="prix"></label>
            <input name="prix" type="text" id="prix" class="prix"  value="<?php echo $row_Recordset1['prix']; ?>"></td>/* it is FLAT in DB*/
            <td><label for="quantity"></label>
            <input name="quantity" type="number" id="quantity" class="quantity"  value="1"></td>
            <td>
              <label for="ptotal"></label>
            <input type="text" name="ptotal" id="ptotal" class="ptotal"  value=""></td>
             <td><label for="poid"></label>
            <input name="poid" type="text" id="poid" class="poid"  value="<?php echo $row_Recordset1['poid']; ?>"></td>/* it is integer in DB*/
            <td>
             <td><label for="weight"></label>
            <input name="weight" type="text" id="weight" class="weight"  value=""></td>
            <td>
          </tr>
          <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
          <tr>
          <td>
              <label for="totalofptotal">Items total price</label>
            <input type="text" name="totalofptotal" id="totalofptotal" class="totalofptotal" value=""></td>
         </tr>
         <tr>
          <td>
              <label for="totalofeight">POID TOTAL</label>
            <input type="text" name="totalofweight" id="totalofweight" class="totalofweight" value=""></td>
         </tr>
        <tr>
        <td>
         <label for="wholeqtty"></label>
            <input type="text" name="wholeqtty" id="wholeqtty" class="wholeqtty" value=""></td>
            <!--EN TES DU STRUCTURE-->
            </td>
            <tr>
            <td> <label for="shipping">SHIPPING</label>
      <select name="Shipping" class="shipping" id="shipping"> Shipping
        <option  value="0" selected="selected">0.00</option>
           <option value="0.0068">0.0068</option>
           <option value="0.026">0.026</option>
           <option value="0.03076">0.3076</option>
         </select>
<label for="shippingtotal"></label>
      <input type="text" name="shippingtotal" id="shippingtotal" value=""><br></td>
      </td>
      </tr>
      <tr>
      <td>
<label for="total">TOTAL</label>
      <input type="text" name="grandtotal" id="grandtotal" class="grandtotal" value="">
      </td>
         </tr>
      </table>
    </form>

and here is the actual script

<script type="text/javascript">
function calculatePtotal() {
    var totalofptotal = 0;//initialise the accumulator
    var wholeqtty = 0; //initialise the acumulator of quantity
    var totalofweight = 0; //initialise the acumulator of weight
    $("tr.calc", "#myTable", "#panierform").each(function (i, row) {
        $row = $(row);
        var value = parseFloat($('.prix', $row).val());
        var quantity = parseFloat($(".quantity", $row).val());
        var poid = parseInt($(".poid", $row).val());
        var shipping = parseFloat($(".shipping", $row).val());
        var ptotal = (value * quantity).toFixed(2);//calculate row total
        var weight = (poid * quantity) .toFixed(0);//calculate row weight
        $(".quantiy", $row).val(quantity);//display row total
        $(".ptotal", $row).val(ptotal);//display row total
        $(".weight", $row).val(weight);//display row total
        totalofptotal += parseFloat(ptotal);//accumulate the row totals to make a grand total
        wholeqtty += parseInt(quantity);
        totalofweight += parseInt(weight);
    });
    $("#wholeqtty") .val(wholeqtty.toFixed(0));
    $("#totalofweight") .val(totalofweight.toFixed(0));
    $("#totalofptotal").val(totalofptotal.toFixed(2));//display the product total
}
{
    var shippingtotal = parseFloat(shipping * totalofweight);

    $("#shippingtotal").val(shippingtotal.toFixed(2));//display row total
}
calculatePtotal();
$(".quantity, .shipping").change(function () {
    calculatePtotal();
});
</script>

first of all remove the '{}' at line 24 and 25 of the code since line 26 to 28 is part of the function calculatePtotal;
here is the corrected code

<script type="text/javascript">
function calculatePtotal() {
    var totalofptotal = 0;//initialise the accumulator
    var wholeqtty = 0; //initialise the acumulator of quantity
    var totalofweight = 0; //initialise the acumulator of weight
    $("tr.calc", "#myTable", "#panierform").each(function (i, row) {
        $row = $(row);
        var value = parseFloat($('.prix', $row).val());
        var quantity = parseFloat($(".quantity", $row).val()) || 0;
        var poid = parseInt($(".poid", $row).val());
        var shipping = parseFloat($(".shipping", $row).val());
        var ptotal = (value * quantity).toFixed(2);//calculate row total
        var weight = (poid * quantity) .toFixed(0);//calculate row weight
        $(".quantiy", $row).val(quantity);//display row total
        $(".ptotal", $row).val(ptotal);//display row total
        $(".weight", $row).val(weight);//display row total
        totalofptotal += parseFloat(ptotal);//accumulate the row totals to make a grand total
        wholeqtty += parseInt(quantity);
        totalofweight += parseInt(weight);
    });
    $("#wholeqtty") .val(wholeqtty.toFixed(0));
    $("#totalofweight") .val(totalofweight.toFixed(0));
    $("#totalofptotal").val(totalofptotal.toFixed(2));//display the product total
    var shippingtotal = parseFloat(shipping.value) * totalofweight
    $("#shippingtotal").val(shippingtotal.toFixed(2));//display row total
}
calculatePtotal();
$(".quantity, .shipping").change(function () {
    calculatePtotal();
});
</script>

for some strange reason the value is shipping at line 26 is still a dom element.
but the code should work

it work thank you very much. :) i would mark as solved but i have just 2 question, why we use a value in line 24 what it mean?? i know only val in jquery, (and i'm sorry for my stupid question a simple answer anought for me)
and one last thing i have a html field which i want subtrack on totalofptotal
but when i do var offer = parseFloat($(".offer", $row).val()); AND totalofptotal += parseFloat(ptotal)+ offer;
and i get NaN again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.