我新使用的HTML,JavaScript,PHP和Smarty。從點擊事件數據庫中獲取價格
我想知道如何讓我的價格顯示,如果產品被選中。
我的數據庫連接工作,我可以顯示產品,但是當我選擇產品時,我希望他的價格在窗體中顯示。
.PHP
<?php
$new = ['product_id','product_category','product_price','product_quantity','product_about','product_color'];
//Database connection
$db = mysqli_connect('xxx','xxx','xxx','xxx')
or die('Error connecting to MySQL server.');
//access Smarty template engine
require_once('Smarty-3.1.30/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'views';
$smarty->compile_dir = 'tmp';
//query product page
$query = "SELECT * FROM cs_shop";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
//query an array of products
$rows = array();
//loop start
while ($row = mysqli_fetch_array($result)) {
$rows[] = array(
'product_id' => $row['product_id'],
'product_category' => $row['product_category'],
'product_price' => $row['product_price'],
'product_quantity' => $row['product_quantity'],
'product_about' => $row['product_about'],
'product_color' => $row['product_color']
);
}
//db collect data
$smarty->assign('row', $rows);
//template
$smarty->display('index.tpl');
mysqli_close($db);
?>
的.js
<script>
$(document).ready(function() {
//your code here
$(function() {
$('#products').change(function() {
$('#priceInput').val($('#products option:selected').data('data-price'));
});
});
});
</script>
.tpl
<select name="productID" id="products">
{foreach from=$row item="item"}
<option value="$item['product_category']" data-price="$item['product_price']" >{$item['product_category'] } : {$item['product_price'] }</option>
{/foreach}
</select>
<form>
Price : <input value="" name="Price" type="text" id="priceInput" disabled="disabled">
</form>
我想知道我是缺什麼這裏?在此先感謝
您可能需要圍繞PHP值大括號中的'數據價格=「$項目[‘PRODUCT_PRICE’]」'到actualy打印價格值到模板 - >'數據價格=「{$項目[」 product_price']}「' – insider
我加了,但沒有顯示.. – Ylama