dragon321723 2012-05-30 04:59
浏览 57
已采纳

JQuery - Orderform,自动更新价格,存储信息

Im using Wordpress as a CMS for this website. Basically the site has products that are amongst different categories. The products are stored as a custom post type called 'product'

On the front-end, there's going to be an order form. What I'm trying to figure out is this: The client wants to have the 'order totals' updated on the fly. So if a user types in '5' for a particular product, it'll update that total after the keystroke.

Furthermore, a product/service could potentially be in different categories, which would effect the price. For example

Product: Wash & Wax

Categories: Full Service | Once Over

So if the product 'Wash & Wax' has an text <input>, and below it are those categories with corresponding radio buttons, and a user chooses 'Full Service' which might be $10 for THAT product, or 'Once Over' which might be $7 for THAT product, I'm trying to figure out:

what the best way is to pull the data from the back-end,

store it in some sort of JQuery accessible Object / Array,

and have the Product inputs access their corresponding prices from that data set

Currently, prices are stored on a PER PRODUCT basis in the database. So the product 'Wash & Wax (full service) is one row in the DB with its own price, and 'Wash & Wax' (once over) is a separate product in the DB with its own price.

EDIT:

If the question is too specific, really what Im asking is: Whats the best way to pull MySQL data, for use in a dynamic context (like updating price totals with JQuery) and whats the best way to link up the HTML inputs with the fetched data?

  • 写回答

1条回答 默认 最新

  • dsh12544 2012-06-04 23:49
    关注

    I decided to make everything simpler. I got rid of the Categories being part of the front end equation. I just pulled all Products from the database along with their unique Slug and Price. The array is setup simply as:

    $products = array(
         'the-unique-slug' => array(
              'id' => $id,
              'price' => $price,
         'next-unique-slug' => array(
         etc......
    

    This way, even if the categories change, the Slug will always be the same and unique.

    Next, I decided to display ALL separate text <inputs> for tall the variations on a certain product. I hide all of them except using jQuery. I then put the Categories in classes:

    <input type='text' class='raw standard product-input' name='photo-prints-1' />
    <input type='text' class='direct standard product-input' name='photo-prints-2' />
    etc..
    

    So when a user changes to the 'standard' radio button and 'raw' radio button, JQuery simply show() those <inputs> possessing those classes, and hide() anything that doesn't

    And lastly, to get the array in to jQuery, I just Json_encoded() it:

    <script type="text/javascript"> var products = <?php echo json_encode($products) ?>;</script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值