简体   繁体   中英

Get price from database on click event

Im new to using html, javascript, php and 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

$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>

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

.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". What i had was "data-data-price".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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