get total for selected record every time when checking checkbox using JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Comskiesci
    New Member
    • Jan 2013
    • 3

    get total for selected record every time when checking checkbox using JavaScript

    Code:
    <div style="background-color:#fffcc0; font-family:Arial, Helvetica, sans-serif; color:#000000; padding:5px; margin:auto; width:400px; height:auto;"> 
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("studentrecord", $con);
    
    $result = mysql_query("SELECT * FROM miscellaneous");
    
    
    
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<th scope='row' class='spec'>" . '<input name="selector[]" type="checkbox" value="'.$row['Misc_Code'].'">' . "</th>";
    // echo "<th scope='row' class='spec'>" . '<input name="selector[]" type="checkbox" value="'.$row['Misc_Code'].'">' . "</th></br>";
    echo "&nbsp;&nbsp;<td>" . $row['Misc_Code'] . "</td></br>";
    echo "</tr>";
    }
    echo "</table>";
    
    mysql_close($con);
    ?>
    </div>
    Last edited by Dormilich; Feb 4 '13, 06:33 AM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    since you posted in the JavaScript forum, what’s with all those PHP code?

    Comment

    • Comskiesci
      New Member
      • Jan 2013
      • 3

      #3
      i want to use some javascript code so that every time when i check some record it will automatically get the total value of the selected record...pls help

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        Please note that th tag is used to add table header. You are not supposed to use it for table data. You should use td instead.

        Regarding JS, adding to Dormilich say: what is your JS problem to help you in? We are not supposed to give you help with a complete solution without especially if you don't start helping yourself first. Give it a try and if you get stuck, post and we will see how we can help.

        Comment

        • Comskiesci
          New Member
          • Jan 2013
          • 3

          #5
          Code:
          <script type="text/javascript">
          function checkTotal() {
          document.listForm.total.value = '';
          var sum = 0;
          for (i=0;i<document.listForm.choice.length;i++) {
          if (document.listForm.choice[i].checked) {
          sum = sum + parseInt(document.listForm.choice[i].value);
          }
          }
          document.listForm.total.value = sum;
          }
          </script>
          
          <form name="listForm">
          <input type="checkbox" name="choice" value="2" onchange="checkTotal()"/>2<br/>
          <input type="checkbox" name="choice" value="5" onchange="checkTotal()"/>5<br/>
          <input type="checkbox" name="choice" value="10" onchange="checkTotal()"/>10<br/>
          <input type="checkbox" name="choice" value="20" onchange="checkTotal()"/>20<br/>
          Total: <input type="text" size="2" name="total" value="0"/>
          </form>
          -----------------------------------------------
          instead of the value above, now i want that value coming from mysql database. This is my code below getting record from sql

          Code:
          <?php
          $con = mysql_connect("localhost","root","");
          if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }
          
          mysql_select_db("studentrecord", $con);
          
          $result = mysql_query("SELECT * FROM miscellaneous");
          
          
          
          while($row = mysql_fetch_array($result))
          {
          echo "<tr>";
          echo "<th scope='row' class='spec'>" . '<input name="selector[]" type="checkbox" value="'.$row['Misc_Code'].'">' . "</th>";
          
          echo "&nbsp;&nbsp;<td>" . $row['Misc_Amount'] . "</td></br>";
          echo "</tr>";
          }
          echo "</table>";
          
          mysql_close($con);
          ?>
          Last edited by Rabbit; Feb 5 '13, 03:29 AM. Reason: Please use code tags when posting code. Second warning.

          Comment

          Working...