I was wondering if anyone could kindly help me or steer me in the right direction with this one...Basically I have a Javascript function which calculates the total number between a ticket price (from mysql database) and the quantity (from a drop-down menu). The Javascript and PHP work fine...but only for the first result, the functionality is not dynamic and really need help fixing this.
In my PHP, a table is returned from an SQL statement:
$postCodeSQL = "SELECT * FROM ticket WHERE locationID IN (SELECT locationID FROM location WHERE postCode LIKE '$pcSearch') ";
$postCoderesult = mysql_query($postCodeSQL) or die(mysql_error());
while ($pcRow = mysql_fetch_assoc($postCoderesult)) {
$ID = $pcRow['ticketID']; //row in ticket table on database
$venue = $pcRow['venue'];
$ticketPrice = $pcRow['tPrice'];
$date = $pcRow['date'];
$time= $pcRow['time'];
echo "<tr>\n";
echo "<td id=\"venuePlace\">$venue <input type='hidden' id='venuePlaceHidden' class='venuePlaceHidden' value='".$venue."'></td>\n";
echo "<td id=\"ticketprice\">£$ticketPrice <input type='hidden' id='ticketPriceHidden' class='ticketPriceHidden' value='".$ticketPrice."'></td>\n";
echo "<td >
<select name =\"showQuantity\" id=\"showQuantity\" class =\"showQuantity\" onChange=\"calculateCost()\" >
<option value=\"Select\">Select...</option>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</td>\n";
echo "<td id=\"date\">$date <input type='hidden' id='dateHidden' class='dateHidden' value='".$date."'></td>\n";
echo "<td id=\"time\">$time <input type='hidden' id='timeHidden' class='timeHidden' value='".$time."'></td>\n";
echo "<td> <input type= \"button\" class= \"buyMe\" value= \"Buy\" name=\"buyMe\" onclick=\"grandTotal()\" >
</td>\n";
echo "</tr>\n";
}
The hidden fields are inserted so the Javascript functions can parse these values. The JS function parses these values and multiplies the cost of the ticket ($ticketPrice) and multiplies it a delivery value:
function quantityChange() {
//Select the value of the drop down list
var quantity = $('.showQuantity option:selected').val();
//get the value of the number from the tPrice column in 'ticket' table
var ticket = parseInt(document.getElementById('ticketPriceHidden').value);
//get the venue value
var venue = document.getElementById('venuePlaceHidden').value;
var date = document.getElementById('dateHidden').value;
var time = document.getElementById('timeHidden').value;
//multiply them together
var total = quantity * ticket;
return {
venue: venue,
quantity: quantity,
ticket: ticket,
date: date,
time: time,
total: total
};
}
function calculateCost () {
var data = quantityChange();
$('#summary').html('You have chosen '+ ' ' + data.quantity + ' ' + 'tickets @' + ' ' + data.venue + ' = ' + ' ' + '£' + data.total);
}
As stated before, this function works for the top result returned by the table, however in my table there are more than one result returned and the JS doesn't work on these values. my problem is related with duplicate IDs because each result returned has the same ID.
I've given each result a class but I cannot do a 'getElementByClass' method. Please please please does anyone know how to edit this code so the JS functions will work for the other results returned? I have a feeling that I may possibly need a foreach() loop to state 'for each value that is returned, apply the JS function...' or something along those lines?
Any help is really appreciated and I am working on improving my accept rate!