我在想,如果有人能帮我,或指引我走正确的方向...我基本上有一个 Javascript 函数来计算票价总额(从 Mysql 数据库) 和数量(从下拉菜单)之间的总数字。 Javascript 和 PHP 工作精细... 但只有第一个结果, 功能不是动态的, 并且真的需要帮助来解决这个问题 。
在我的PHP中, SQL 语句中返回一张表格:
$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>
";
echo "<td id="venuePlace">$venue <input type= hidden id= venuePlaceHidden class= venuePlaceHidden value= ".$venue." ></td>
";
echo "<td id="ticketprice">£$ticketPrice <input type= hidden id= ticketPriceHidden class= ticketPriceHidden value= ".$ticketPrice." ></td>
";
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>
";
echo "<td id="date">$date <input type= hidden id= dateHidden class= dateHidden value= ".$date." ></td>
";
echo "<td id="time">$time <input type= hidden id= timeHidden class= timeHidden value= ".$time." ></td>
";
echo "<td> <input type= "button" class= "buyMe" value= "Buy" name="buyMe" onclick="grandTotal()" >
</td>
";
echo "</tr>
";
}
插入隐藏字段, 使 Javascript 函数能够解析这些值。 JS 函数剖析这些值, 并乘以票价( ticketPrice) 的成本, 并乘以交付值 :
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);
}
如前所述,这一功能对表返回的最高结果起作用,但在我的表格中,不止一个结果被退回,联署材料对这些价值不起作用。 我的问题与重复的身份证有关,因为每个返回的结果都有相同的身份证。
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?
任何帮助都是感激不尽的,我正在努力提高我的接受率!