[英]Get price from database on click event
我是使用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>
我想知道我在这里想念什么吗? 提前致谢
$(document).ready(function () {
$('#products').change(function () {
$('#priceInput').val($('#products option:selected').data('price'));
});
});
因为我在Java脚本上使用了.data,所以我两次读取了数据的函数,那是我的错误..所以修复程序看起来像这样->
.js
$(document).ready(function () {
$('#products').change(function () {
var price = $('#products option:selected').data('price');
$('#priceInput').val(price);
});
});
因此,基本上,据我了解,当它在.tpl中读取“价格”时,会将“数据”添加为值,因此看起来像“数据价格”。 我所拥有的是“数据-数据-价格”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.