0
votes

I have a webpage where the user can search and select for a specific type of paper based on the item selected from the drop down list. The search will then return and display the full name of the paper alongside the price and the per pack value which are all pulled from my database. I need to calculate ( preferably dynamically) the total price which is the (quantity * price)/per_pack. So when the user changes the quantity the total price changes. I have come across several solutions which work within their own scenario however, i was wondering if there was a way to do this without having to specify the price in the value attribute for example:

Not like this:

<input type="text" name="price" id="price" class="txt" size="10" maxlength="9" value="250" />

I have tried doing something like this but it just doesn't seem to work, as in when i enter a quantity nothing seems to happen and the total box is just blank:

JavaScript:

$(function() {
window.globalVar = 0;

// Skip the filled description boxes
for (var i=0; i<10; i++)
{
    if($('#description_'+window.globalVar).val() != "")
    {
        window.globalVar++;
    }
}

// Write the paper description and price for the selected paper
function log( message ) {
    var values = message.split('|');
    $('#description_'+window.globalVar).val(values[0]);
    $('#priceper_'+window.globalVar).val(values[1]);
$('#per_pack_'+window.globalVar).val(values[2]);
    window.globalVar++;
    console.log(window.globalVar);
}

// Search the Paper db
$( "#supplier_search" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://mpc.vario/mis_pp/common/pp_json",
      dataType: "jsonp",
      data: {
        featureClass: "P",
        style: "full",
        maxRows: 25,
        name_startsWith: request.term,
        supplier: $('#supplier').val()
      },
      success: function( data ) {
        console.log(data);
        response( $.map( data, function( item ) {
          return {
            label: item.name,
            value: item.name + '|' + item.value
          }
        }));
      }
    });
  },
  minLength: 2,
  select: function( event, ui ) {
    log( ui.item.value );
    $(this).val('');
    return false;
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});
});

// Calculate total
var sum = 0;
document.getElementById('priceper_0').value = sum;
function OnChange(value)
{
price = document.getElementById('priceper_0').value;
    per_pack = document.getElementById('per_pack_0').value;
quantity = document.getElementById('quantity_0').value;
sum = (price * quantity)/per_pack;


}

HTML:

<tr class="multipp">
<td><input type="text" name="description_0" id="description_0" size="85" maxlength="70" value="<?php echo htmlspecialchars($description[0]); ?>" /></td>
<td><input type="text" name="priceper_0" id="priceper_0" size="10" maxlength="9" value="" /></td>
<td><input type="text" name="per_pack_0" id="per_pack_0" class="txt" size="10" maxlength="9" value="" /></td>
<td><input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="" onchange="return OnChange();" /></td>
<td><input type="text" name="subtotal_0" id="subtotal_0" class="txt" size="10" maxlength="9" value="" /></td>
</tr>
2
document.getElementById('price').value = sum; have you tried to use onkeyup?CaffeineShots
@Strawberry because the prices are pulled out from the database using mysql statements in phpuser3009232
Can u share the specific html code?K P
share your html code as well as full javascriptuser7789076

2 Answers

1
votes

you are calling js function at wrong place, change:

<input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="OnChange()"/>

to

<input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="" onchange="return OnChange();"/>
1
votes

The issue is in calling method

replace the following

 value="OnChange()"

to

 onchange="OnChange()"