php - 使用ajax根据所选选项检查价格

标签 php mysql ajax

我正在尝试根据用户选择的选项更改价格。我从数据库中获取价格和尺寸。它们是一种使用ajax根据用户从下拉选项中选择的bsize更改价格的方法吗?在我的数据库中,我有

表名:大小

  Sid       bsize.  Sprice  
  1         12         200  
  2         14         250  
  3         16         400  
<div class="soat_r">
    <form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
        <table>
            <tr>
                <td width="160">Price:</td>
                <td name="price"></td><!-- the price will go here and will change depend on the bsize the choose -->
            </tr>
            <tr>
                <td for="size" width="160">size*:</td>
                <td>
                    <select name="size" class="small" id ="size">
                    <!-- I will echo out the bsize here and it will be in a drop down <option> -->
                    </select>
                </td>
            </tr>   
            <tr>

            </tr>   
            <tr>
                <td>Quantity</td>
                <td><input type="text" name="Qty" value="1" style="width: 20px; text-align: right" /></td>
            </tr>       
        </table>    
        <div class="cleaner h20"></div>

        <input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
        <input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
    </form>             
</div>

他们的产品在不同的表中列出,所以我有 1 Adidas blue 2. addias white 3. addidias green 然后在不同的表上我有价格... addidas 的价格根据尺寸变化...这就是为什么我有一个尺寸和价格表...在购物车页面上我发布了名称、选择的尺寸和价格(取决于尺寸)...假设您进入蓝色 addias 页面。你选择尺码,我希望它能告诉你该尺码在价格表上的价格……你不必添加到购物车来找出……我试过这样做,但我没有主意,我如果有人能给我一些代码就太好了……谢谢

最佳答案

当然,

如果您使用 jQuery:

$('#size').change(function() {
    $.post("get_price_by_bsize.php", "bsize="+$("#size").val())
        $("#price_td").html(data);
    });
});

get_price_by_bsize.php 上运行一个查询:

SELECT Sprice from size WHERE bsize={$_POST['bsize']}

并打印价格。不要忘记为价格分配一个 id <td> .

没有 jQuery ,相同的主体,但有 xmlhttprequestonclick

关于php - 使用ajax根据所选选项检查价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008863/

相关文章:

php - PHP 文件是否有可能在执行时删除自身?

javascript - 如何使用 PHP、AJAX 和 AngularJS 创建实时聊天?

mysql - 如何在 MySql 中检查 zer0 时间戳?

javascript - 多次 AJAX 调用,无阻塞

javascript - 当链接在新选项卡中打开时如何保留在当前窗口选项卡上

php - 匹配php中单独列表上的值

php - Phalcon MySQL 按位查询(左移运算符)

php - 源码编译php致命PDO异常;找不到驱动程序

mysql - varchar(255) vs tinytext/tinyblob 和 varchar(65535) vs blob/text

javascript - 防止浏览器卡在 AJAX 请求上