I'm trying to create a simple food cart which has; options i.e. small, medium, large and extras i.e. cheese, onions, chilli's.
I have a script which calculates the amounts however the script is within a while loop for each food item, I'm having some difficulty making each call for the script unique.
This is what I have so far: http://jsfiddle.net/2F8LA/1/
I have tried using echo $testnumber next to each id within the PHP and JS to make them unique but, it doesn't work. Think I'm in a little over my head :)
How can I make each loop unique please? Thanks in advance.
<?php
$testnumber = 5;
?>
<?php do { ?>
<?php $testnumber -= 1;?>
<hr />
<div>
<div>
<select id="options" name="options" class="options">
<option value="10.00">$10.00</option>
<option value="20.50">$20.50</option>
<option value="30.80">$30.80</option>
</select><span style="margin-left:30px;">TOTAL: $<span id="totalamount"> </span></span>
</div>
<br />
<div>
<input type="checkbox" name="extra" value="0.50" />$0.50
<input type="checkbox" name="extra" value="1.50" />$1.50
<input type="checkbox" name="extra" value="0.80" />$0.80
</div>
</div><br />
<script type="text/javascript">
// for adding the options and extras
function displayVals() { // get option value
calcUsage();
var singleValues = $("#options").val();
}
var $cbs = $('input[name="extra"]');
function calcUsage() {
var total = $("#options").val();
$cbs.each(function() {
if(this.checked)
total = (parseFloat(total) + parseFloat(this.value)).toFixed(2);
});
$("#totalamount").text(total); // display total
}
$("#options").change(displayVals);
displayVals();
//For checkboxes
$cbs.click(calcUsage);
</script>
<?php }while($testnumber > 0); ?>
<hr />
Thank you Ohgodwhy I now have the following code but, I don't understand how to connect "displayVals(ele)" to "displayVals($(this))" sorry I'm new to Jquery/Javascript.
If you don't have time to explain I understand but, if you have a link to somewhere I can learn about what this means that would be great.
<head>
<script type="text/javascript" src="scripts/jquery/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function(){
$(document).on('change', 'select[name^=options]', function(){
displayVals($(this));
});
$(document).on('click', 'input[name^=extra]', function(){
calcUsage($(this));
});
});
</script>
</head>
<?php $testnumber = 5; ?>
<?php do { ?>
<?php $testnumber -= 1; ?>
<hr />
<div>
<div>
<select id="options" name="options-<?php echo $testnumber; ?>" class="options">
<option value="10.00">$10.00</option>
<option value="20.50">$20.50</option>
<option value="30.80">$30.80</option>
</select><span style="margin-left:30px;">TOTAL: $<span id="totalamount-<?php echo $testnumber; ?>"> </span></span>
</div>
<br />
<div>
<input type="checkbox" name="extra-<?php echo $testnumber; ?>[]" value="0.50" />$0.50
<input type="checkbox" name="extra-<?php echo $testnumber; ?>[]" value="1.50" />$1.50
<input type="checkbox" name="extra-<?php echo $testnumber; ?>[]" value="0.80" />$0.80
</div>
</div><br />
<script type="text/javascript">
function displayVals(ele) { // get option value
calcUsage(ele);
var singleValues = ele.val();
}
function calcUsage(ele) {
var total = ele.val(),
$cbs = ele.parent().parent().find('input[name="^extra"]'),
cnt = cbs.prop('name').split('-')[1]; // the iteration number
$cbs.each(function() {
if(this.checked) total = (parseFloat(total) + parseFloat(this.value)).toFixed(2);
});
$("#totalamount-"+cnt).text(total); // display total
}
</script>
<?php }while($testnumber > 0); ?>
<hr />