简体   繁体   English

在点击事件中从数据库获取价格

[英]Get price from database on click event

Im new to using html, javascript, php and Smarty. 我是使用html,javascript,php和Smarty的新手。

I would like to know how to get my price to display if a product is selected. 我想知道如何在选择产品时显示我的价格。

My database connection works i can display the products , but when i select the product i want his price to display in a form. 我的数据库连接有效,我可以显示产品,但是当我选择产品时,我希望他的价格以表格形式显示。

.php .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 .js

<script>

$(document).ready(function () {
  //your code here
  $(function () {
        $('#products').change(function () {
        $('#priceInput').val($('#products option:selected').data('data-price'));
    });
});

});

</script>

.tpl .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>

I would like to know what i am missing here? 我想知道我在这里想念什么吗? thanks in advance 提前致谢

$(document).ready(function () {
        $('#products').change(function () {
        $('#priceInput').val($('#products option:selected').data('price'));
    });
});

Becuase i used .data on my java script i read function the data twice, and that was my mistake.. so the fix looks like this --> 因为我在Java脚本上使用了.data,所以我两次读取了数据的函数,那是我的错误..所以修复程序看起来像这样->

.js .js

$(document).ready(function () {

    $('#products').change(function () {
        var price = $('#products option:selected').data('price');
    $('#priceInput').val(price);

});

});

So basically to my understanding when it reads 'price' in my .tpl it adds 'data' as value so it would look like "data-price". 因此,基本上,据我了解,当它在.tpl中读取“价格”时,会将“数据”添加为值,因此看起来像“数据价格”。 What i had was "data-data-price". 我所拥有的是“数据-数据-价格”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM