468,490 Members | 2,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

how can I sum the price of the item that I selected from my dropdown that retrieved i

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     //database connection here
  3. ?>
  4. <table class = "TOTAL" border =" 1">
  5.  
  6.     <thead>
  7.         <tr>
  8.             <th>Component</th>
  9.             <th>Item Name   </th> 
  10.             <th>Price </th>  
  11.  
  12.         </tr>
  13.     </thead>
  14.     <tbody>
  15.         <tr>
  16.             <td>CPU</td>
  17.             <td>
  18.                 <?php
  19.                 //Retrieving CPU table
  20.                     $query = $conn->query("SELECT * FROM cpu");
  21.                         echo '<select  onChange = parseFloat($("#cpuprice").val($(this).find("option:selected").attr("cpuprice")))>';
  22.                         echo "<option>---select your CPU---</option>";
  23.                             while ($obj = $query->fetch_object()){
  24.                                 echo '<option cpuprice = "'.$obj->price.'" >'.$obj->cpuname.'</option>';                                
  25.                             }
  26.                         echo '</select>';
  27.                 ?>                                                 
  28.             </td>
  29.             <td>
  30.             <output id="cpuprice" disabled value="">    
  31.             </td>
  32.         </tr>   
  33.     </tbody>                            
  34.     <tbody>
  35.         <tr>
  36.             <td>GPU</td>
  37.             <td>
  38.                 <?php
  39.                 //Retrieving GPU table
  40.                     $query = $conn->query("SELECT * FROM gpu");
  41.                         echo '<select  onChange = parseFloat($("#gpuprice").val($(this).find("option:selected").attr("gpuprice")))>';
  42.                         echo "<option>---select your GPU---</option>";
  43.                             while ($obj = $query->fetch_object()){
  44.                                 echo '<option  gpuprice = "'.$obj->price.'" >'.$obj->gpuname.'</option>';                               
  45.                             }
  46.                         echo '</select>';
  47.                 ?>                                     
  48.             </td>
  49.             <td>
  50.             <output id="gpuprice" disabled value="">    
  51.             </td>
  52.         </tr>   
  53.     </tbody>
  54.     <tbody>
  55.         <tr>
  56.             <td></td>
  57.             <td>                                    
  58.             </td>
  59.             <td>
  60.                 <span class="totalprice">Total: </span>
  61.                 <script> 
  62.                         // i tried this but it always return NaN after selecting an item
  63.                     $('select').change(function() {     
  64.                         var total = 0;                          
  65.                         var cpu = parseInt($(total).val($(this).find("option:selected").attr("cpuprice")))
  66.                         var gpu = parseInt($(total).val($(this).find("option:selected").attr("gpuprice")))
  67.                         total = parseInt(cpu) + parseInt(gpu);
  68.                     $('.totalprice').text('₱'+ total);                       
  69.                     })
  70.                 </script>                 
  71.             </td>
  72.         </tr>   
  73.     </tbody>
  74. </table>
Attached Images
File Type: png af.png (9.2 KB, 12 views)
Dec 9 '20 #1
0 1811

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Big Dave | last post: by
1 post views Thread by Rudy Ko | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.