dtgj8529 2015-09-11 06:50
浏览 43

如何获得所选产品的价格并在桌面上显示?

so the requirement is to have a table which allows the user to create a new estimate online. I went as far as adding rows/ deleting them, having my DB's product list on the product field. But once I select the product, I would like the field 'Price' to pull up its price.

Here's the code I am currently using:

<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

<TABLE id="dataTable" width="350px" border="1">
    <TR>
        <TD><INPUT type="checkbox" name="chk"/></TD>
        <TD><INPUT type="text" name="txt"/></TD>
        <TD>
            <SELECT name="country" onchange="getPrice(this.value, this.closest('tr').rowIndex);">
                <?php while($row = mysql_fetch_array($query)) { 

                echo '<option value="' .$row['nom'] . '">'.$row['nom'].'</option>'; }

                ?>


            </SELECT>
        </TD>

        <TD><INPUT type="text" name="txt"/></TD>


    </TR>
</TABLE>

  • 写回答

1条回答 默认 最新

  • dtziv24262 2015-09-11 09:58
    关注

    Ok, so what I assume is that your javascript function getPrice(name, tablecell) should retrieve the price stored in your database and puts it's price-value into the given table cell.

    Before I write out a possible solution, I would like to add: This solution will keep the values from the database in a client-side array. Thus visible for any user, reviewing the code of your page. This shouldn't be an issue if you only store non-critical information though.

    // For testing purpose i filled in a mini-json, but in  
    // the PHP-example later this post you will find a short
    // example how to fill this with PHP from your database
    var prices = { 
      'test_prod_1' : 16.46, 
      'test_prod_2' : 21.55,
      'test_prod_3' :  7.4
    };
    
    
    function getPrice( prodID, tableCell )
    {
      if (typeof prices[prodID] !== 'undefined')
        tableCell.innerHTML  = '&euro;' + prices[prodID];  // Get the price from the pre-loaded price information
      else
        tableCell.innerHTML  = '?';
        
    }
    <input type="button" value="click me" onClick="javascript:getPrice('test_prod_2', document.getElementById('test_cell'));">
    
    <table border=1 cellspacing=0 cellpadding=5>
      <tr>
        <td>Product name</td>
        <td>price</td>
      </tr>
      
      <tr>
        <td>test</td>
        <td id="test_cell"></td>
      </tr>
    </table>

    And then with PHP You would be able to create this price list dynamically. But, remember that the data is visible client side, so this example will only select the price and name/id data. Make sure to edit the variable names to the ones you use.

    <script type="text/javascript">
    
        var prices = <?php
    
        $js_pricelist = array();
    
        // Retireve the information from the server's database. Assume that $result is a result set in an ASSOC
        foreach( $result as $row )
            $js_pricelist[ $row['prodID'] ] = $row['prodPrice'];
    
        echo JSON_Encode( $js_pricelist );
    
        ?>
    </script>
    

    Please check the last script as I haven't tested it yet, and is meant more to give you an idea of the possibilities

    Conclusion: The main idea is, that once you load the page, you assemble a javascript object containing all price data (Using PHP). You echo this object to a javascript block, and make a function that reads from this newly created array/object to retrieve the price that corresponds to the product_id.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)