skip to Main Content

I have this code to make some calculation when I change the inputs fields:

<div class="card-body">
  <div class="form-group">
    <label>Pret vanzare</label>
    <input id="price_vanzare" type="text" name="price" class="form-control" placeholder="Product price" value="<?php echo $product->price; ?>">
  </div>
  <div class="form-group">
    <label>Pret achizitie</label>
    <input id="price_achizitie" type="text" name="price_achizitie" class="form-control" placeholder="Product price" value="<?php echo $product->price_achizitie; ?>">
  </div>
  <div class="form-group">
    <label>Pret transport</label>
    <input id="price_transport" type="text" name="price_transport" class="form-control" placeholder="Product price" value="<?php echo $product->price_transport; ?>">
  </div>
  <div class="form-group">
    <label>Profit net</label>
    <input id="profit" type="text" class="form-control">
  </div>
</div>

and jQuery code:

$(":input[type='text']").keyup(function(){ 
    
        var pret_achizitie = $('#price_achizitie').val();
        var pret_vanzare   = $('#price_vanzare').val();
        var pret_transport = $('#price_transport').val();

        var profit_net = ((pret_vanzare - ((pret_achizitie*1.19) + pret_transport + (0.19 * pret_transport))) - ((pret_vanzare * 0.19/1.19) - ((pret_achizitie * 0.19)+(pret_transport * 0.19)))) - (1/100) * (pret_vanzare -(pret_vanzare * 0.19/1.19));    
                
        $('#profit').val(parseFloat(profit_net).toFixed(2));

    } );

Before keyup I have:

var pret_achizitie = <?php echo $product->price_achizitie; ?>;
var pret_vanzare   = <?php echo $product->price; ?>;
var pret_transport = <?php echo $product->price_transport; ?>;
var transport_client = <?php echo $product->transport_client; ?>;

var profit_net = ((pret_vanzare - ((pret_achizitie*1.19) + pret_transport + (0.19 * pret_transport))) - ((pret_vanzare * 0.19/1.19) - ((pret_achizitie * 0.19)+(pret_transport * 0.19)))) - (1/100) * (pret_vanzare -(pret_vanzare * 0.19/1.19));    
$('#profit').val(parseFloat(profit_net).toFixed(2));

and everything is working fine but when I change something is returned NaN for #profit input ID

If I change on var profit_net and put only pret_vanzare - pret_achizitie it working even I change inputs for pret_vanzare or pret_achizitie

I don’t know what is wrong in var profit_net because there is something wrong!

2

Answers


  1. make sure all the values are valid number this (pret_achizitie, pret_vanzare, pret_transport, transport_client).

    if(!isNaN(pret_achizitie) && !isNaN(pret_vanzare) && 
    !isNaN(pret_transport) && !isNaN(transport_client)) {
    
    var net_profit = pret_vanzare - ((pret_achizitie * 1.19) + 
    pret_transport + (0.19 * pret_transport)) - ((pret_vanzare * 0.19 
    / 1.19) - ((pret_achizitie * 0.19) + (pret_transport * 0.19))) - 
    (1 / 100) * (pret_vanzare - (pret_vanzare * 0.19 / 1.19));
    
        $('#profit').val(parseFloat(net_profit).toFixed(2));
    } else {
        $('#profit').val("Invalid Input");
    }
    
    Login or Signup to reply.
  2. <?php
    $pret_achizitie =1;
    $pret_vanzare = 2;
    $pret_transport = 3;
    $transport_client = 4;
    ?>
    
    <script>
    var pret_achizitie = <?php echo $pret_achizitie; ?>;
    var pret_vanzare = <?php echo $pret_vanzare; ?>;
    var pret_transport = <?php echo $pret_transport; ?>;
    var transport_client = <?php echo $transport_client; ?>;
    
     if (!isNaN(pret_achizitie) && !isNaN(pret_vanzare) && 
    !isNaN(pret_transport) && !isNaN(transport_client)) {
    var net_profit = pret_vanzare - ((pret_achizitie * 1.19) + 
     pret_transport + (0.19 * pret_transport)) - ((pret_vanzare * 0.19 / 
    1.19) - ((pret_achizitie * 0.19) + (pret_transport * 0.19))) - (1 / 100) 
    * (pret_vanzare - (pret_vanzare * 0.19 / 1.19));
    
        document.write("Net Profit: " + parseFloat(net_profit).toFixed(2));
    } 
    </script>
    
    
    i am using this in w3school php compiler its working please check in 
    your code. and if your value save in input then use   
          $('#profit').val(parseFloat(net_profit).toFixed(2));
    other wise use   $('#profit').html(parseFloat(net_profit).toFixed(2)); 
    or 
      $('#profit').text(parseFloat(net_profit).toFixed(2));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search