当用户单击不同的产品数量时,我正在尝试对 html 表进行简单更新。虽然对 jQuery 很陌生,但我在使用 ajax POST 方法时遇到了一些问题。
这是我到目前为止所得到的:
test-table.html
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function swapContent(count){
$(".price-sidebar").html("Put animation here").show();
$.ajax({
type: "POST",
url: "myphpscript.php",
data: {countVar: count},
success: function(data){
}
});
}
</script>
</head>
<body>
<a href="#" onClick="return false" onmousedown="javascript:swapContent('18');">18</a>
<a href="#" onClick="return false" onmousedown="javascript:swapContent('48');">48</a>
<a href="#" onClick="return false" onmousedown="javascript:swapContent('96');">96</a>
<section class="price-sidebar span8" >
<h2>Price Comparison</h2>
</br>
<table class="price-data">
<tr>
<th class='store-row'><h4>Store</h4></th>
<th class='price-row'><h4>Price</h4></th>
</tr>
<!--insert returned foreach data -->
</table><!--end price-data-->
</section><!--end price sidebar-->
</body>
myphpscript.php
require('C:\wamp\www\Single-Serve-Coffee\includes\database-connect.php');
$countVar = $_POST['countVar'];
function PriceCompare($countVar){
$STH = $DBH->query('SELECT ProductID, MerchantName, Price, PageURL
FROM merchants
WHERE ProductID=677 AND Count=' . $countVar . '
ORDER BY Price');
$result = $STH->fetchAll();
foreach($result as $row){
echo "<tr>";
echo "<td class='store-row'><h5 property='seller'>" . $row['MerchantName'] . "</h5></td>";
echo "<td class='price-row'><h5 property='price'>$" . $row['Price'] . "</h5></td>";
echo "<td><a property='url' target='_blank' href='" . $row['PageURL'] . "' class='btn btn-danger'>GET IT</a></td>";
echo "</tr>";
echo "</br>";
}
}
?>
我希望 foreach 循环数据显示在 html 页面上,但我不知道如何将它放在那里。我知道我缺少成功函数,但我不知道如何编写 jQuery 从我的 php 脚本中调用 PriceCompare() 函数。