Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate total price
    primarykey
    data
    text
    <p>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 <code>(quantity * price)/per_pack</code>. 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: </p> <p><strong>Not like this:</strong></p> <pre><code>&lt;input type="text" name="price" id="price" class="txt" size="10" maxlength="9" value="250" /&gt; </code></pre> <p>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: </p> <p><strong>JavaScript:</strong> <br /></p> <pre><code>$(function() { window.globalVar = 0; // Skip the filled description boxes for (var i=0; i&lt;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; } </code></pre> <p><strong>HTML:</strong> <br /></p> <pre><code>&lt;tr class="multipp"&gt; &lt;td&gt;&lt;input type="text" name="description_0" id="description_0" size="85" maxlength="70" value="&lt;?php echo htmlspecialchars($description[0]); ?&gt;" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="priceper_0" id="priceper_0" size="10" maxlength="9" value="" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="per_pack_0" id="per_pack_0" class="txt" size="10" maxlength="9" value="" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="" onchange="return OnChange();" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="subtotal_0" id="subtotal_0" class="txt" size="10" maxlength="9" value="" /&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload