Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Auto calculate inside while loop

    Hi...
    I just want to know if it is possible to have an autocalculate in a textboxes inside while loop?and if it is possible how?..

    here is my code:

    Code:
    <html>
    <head>
    <style type="text/css">
    #fieldset_PS{
        position: relative;
        width: 20%;
    }
    </style>
    <link rel="stylesheet" type="text/css" href="kanban.css" /> 
    <script type="text/javascript">
      
    function display_PS(){
        document.loading_kanban.action="ParameterSettings.php";
        document.loading_kanban.submit();
    }
    function display_Kanban(){
        document.loading_kanban.action="kanban_report.php";
        document.loading_kanban.submit();
    }
    </script>
    </head>
     <?php
      error_reporting(0);
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    mysql_select_db("mes", $con);
    
    
    
    ?>
    <form name="loading_kanban">
    <div id="main_button">
    <center>
    <!--<label style="margin-left: .9em; font-family: Arial, Helvetica, sans-serif; font-size: .7em;">Display Details:</label><input  onclick='showDetails(this);' id='chkDetail'   type='checkbox' checked='checked' value='wip'/>     -->
    <input type="button" name="parameter_settings" value="Parameter Settings" onclick="display_PS()">
    <input type="button" name="parameter_settings" value="Stock Requisition"> 
    <input type="button" name="parameter_settings" value="Kanban Report" onclick="display_Kanban()">
    </div>
    <div id="fieldset_PS">
    <fieldset>
    <legend>Parameter Settings</legend>
    </center>  
    <table border="1">
    <th>Compounds</th>
    <th>Max</th>
    <th>Min</th>
    <?php
    $sql = "SELECT PCODE FROM parameter_settings ORDER BY PCODE ASC";
    $result = mysql_query($sql, $con);
    while ($row = mysql_fetch_assoc($result)){
    echo "<tr>
            <td>$row[PCODE]</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr>";
    }   
    ?>
    </fieldset>
    </table>
    </div>
    </form>
    </html>
    I have table : parameter settings and i have fields: PCODE, max, min

    PCODE has data:

    PXX
    PYY
    PZZ
    PAA
    PBB
    Total

    I just want to know how can I auto calculate the total max and total min and it will display in the textbox beside Total...

    Thank you

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Hm, where do I start?

    1. Your form has an id attribute rather than a name attribute. To use the chained format "document.FORMNAME" to access the form, FORMNAME has to be a name attribute
    2. The associative array in PHP has to be addressed by $row["FIELDNAME"] instead of $row[FIELDNAME]
    3. "textbox beside Total"? There is nothing called "Total" in your code ...
    4. "PCODE has data"? PCODE is the content of one single field of your table. How is this associated with data PXX, PYY, PZZ ... and where do you have it on screen? There is no code for that

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Hm, where do I start?

    1. Your form has an id attribute rather than a name attribute. To use the chained format "document.FORMNAME" to access the form, FORMNAME has to be a name attribute
    2. The associative array in PHP has to be addressed by $row["FIELDNAME"] instead of $row[FIELDNAME]
    3. "textbox beside Total"? There is nothing called "Total" in your code ...
    4. "PCODE has data"? PCODE is the content of one single field of your table. How is this associated with data PXX, PYY, PZZ ... and where do you have it on screen? There is no code for that
    I think I need to create separate table for total , total_max and total_min.

    I have sample function autocalculate but my problem is it di not work in textbox inside while loop:

    Code:
    <html>
    <head>
    <style type="text/css">
    #fieldset_PS{
        position: relative;
        width: 20%;
    }
    </style>
    <link rel="stylesheet" type="text/css" href="kanban.css" /> 
    <script type="text/javascript">
      
    function display_PS(){
        document.loading_kanban.action="ParameterSettings.php";
        document.loading_kanban.submit();
    }
    function display_Kanban(){
        document.loading_kanban.action="kanban_report.php";
        document.loading_kanban.submit();
    }
    </script>
    
    <script type="text/javascript">
    var tot;
    function autocalearn(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, total_max = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    {
    field = arguments[a];
    val = parseFloat(field.value); //get value
    alert(val);
    if (!isNaN(val)) //number?
    {
    total_max += val; //accumulate
    alert(total_max);
    }
    }
    //var tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
    
    tot=Number(total_max);
    //alert(tot);
    oForm.total_max.value = tot.toFixed(2);
    
    //oForm.TotEarn.value = TotEarn.toFixed(2); //out
     
    }
    
    </script>
    
    
    </head>
     <?php
      error_reporting(0);
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    mysql_select_db("mes", $con);
    
    
    
    ?>
    <form name="loading_kanban" id= "loading_kanban">
    <div id="main_button">
    <center>
    <!--<label style="margin-left: .9em; font-family: Arial, Helvetica, sans-serif; font-size: .7em;">Display Details:</label><input  onclick='showDetails(this);' id='chkDetail'   type='checkbox' checked='checked' value='wip'/>     -->
    <input type="button" name="parameter_settings" value="Parameter Settings" onclick="display_PS()">
    <input type="button" name="parameter_settings" value="Stock Requisition"> 
    <input type="button" name="parameter_settings" value="Kanban Report" onclick="display_Kanban()">
    </div>
    <div id="fieldset_PS">
    <fieldset>
    <legend>Parameter Settings</legend>
    </center>  
    <table border="1">
    <th>Compounds</th>
    <th>Max</th>
    <th>Min</th>
    <?php
    $sql = "SELECT PCODE FROM parameter_settings ORDER BY PCODE ASC";
    $result = mysql_query($sql, $con);
    while ($row = mysql_fetch_assoc($result)){
    echo "<tr>
            <td>$row[PCODE]</td>
            <td><input type = 'text' name = 'max_pcode' size = '10' onkeyup='return autocalearn(this)'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr>";
    } 
    echo "<tr>
        <td><input type = 'text' name = 'total_max' size = '10' id = 'total_max'></td>  
        <td><input type = 'text' name = 'total_max' size = '10'></td>";
        ?>
    </fieldset>
    </table>
    </div>
    </form>
    </html>

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Please show us the final (browser side) HTML instead of PHP in the first place.

    Second: It's never good to have Javascript variables with the same name as a form field, e.g. "total_max"

    Third: Why are you using "arguments" inside your function if there is always only one parameter oText?

    Fourth: What exactly(!) do you want to accumulate? When exactly do you want that to happen?

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Please show us the final (browser side) HTML instead of PHP in the first place.

    Second: It's never good to have Javascript variables with the same name as a form field, e.g. "total_max"

    Third: Why are you using "arguments" inside your function if there is always only one parameter oText?

    Fourth: What exactly(!) do you want to accumulate? When exactly do you want that to happen?
    my code is mix of php and html code:

    Code:
    <html>
    <head>
    <style type="text/css">
    #fieldset_PS{
        position: relative;
        width: 20%;
    }
    </style>
    <link rel="stylesheet" type="text/css" href="kanban.css" /> 
    <script type="text/javascript">
      
    function display_PS(){
        document.loading_kanban.action="ParameterSettings.php";
        document.loading_kanban.submit();
    }
    function display_Kanban(){
        document.loading_kanban.action="kanban_report.php";
        document.loading_kanban.submit();
    }
    </script>
    
    <script type="text/javascript">
    var tot;
    function autocalearn(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, total_max = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    {
    field = arguments[a];         
    val = parseFloat(field.value); //get value
    alert(val);
    if (!isNaN(val)) //number?
    {
    total_max += val; //accumulate
    alert(total_max);
    }
    }
    //var tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
    
    tot=Number(total_max);
    alert(tot);
    oForm.total_max.value = tot.toFixed(2);
    
    //oForm.TotEarn.value = TotEarn.toFixed(2); //out
     
    }
    
    </script>
    
    
    </head>
     <?php
      error_reporting(0);
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    mysql_select_db("mes", $con);
    
    
    
    ?>
    <form name="loading_kanban">
    <div id="main_button">
    <center>
    <!--<label style="margin-left: .9em; font-family: Arial, Helvetica, sans-serif; font-size: .7em;">Display Details:</label><input  onclick='showDetails(this);' id='chkDetail'   type='checkbox' checked='checked' value='wip'/>     -->
    <input type="button" name="parameter_settings" value="Parameter Settings" onclick="display_PS()">
    <input type="button" name="parameter_settings" value="Stock Requisition"> 
    <input type="button" name="parameter_settings" value="Kanban Report" onclick="display_Kanban()">
    </div>
    <div id="fieldset_PS">
    <fieldset>
    <legend>Parameter Settings</legend>
    </center>  
    <table border="1">
    <th>Compounds</th>
    <th>Max</th>
    <th>Min</th>
    <?php
    $sql = "SELECT PCODE FROM parameter_settings ORDER BY PCODE ASC";
    $result = mysql_query($sql, $con);
    while ($row = mysql_fetch_assoc($result)){
    echo "<tr>
            <td>$row[PCODE]</td>
            <td><input type = 'text' name = 'max_pcode' size = '10' onkeyup='return autocalearn(this)'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr>";
    } 
    echo "<tr>
        <td><input type = 'text' name = 'total_max' size = '10' id = 'total_max'></td>  
        <td><input type = 'text' name = 'total_max' size = '10'></td>";
        ?>
    </fieldset>
    </table>
    </div>
    </form>
    </html>
    Sorry, this function I was search in internet and some code I don't uderstand.

    I only want is everytime I input numbers in a textbox it will autocalculate and display at the last textbox which is total.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I just want to know what is the code for autocalculate which work in my syntax now.

    If it is possible or should I revise my codes.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Quote Originally Posted by newphpcoder View Post
    my code is mix of php and html code:
    That's why I've been asking for the final HTML (as seen in your browser).

    Quote Originally Posted by newphpcoder View Post
    or should I revise my codes
    Most probably yes

    Try to separate PHP from HTML/Javascript when thinking about the program logic. PHP will generate HTML and Javascript in the first place. Then PHP "ceases to exist". All that is left is HTML and Javascript. And that's where your client side logic starts.

    To ensure your Javascript code runs when the rest of the page has been loaded, make sure to put the Javascript code to the bottom of your page (right before the closing </body> tag).

    Then you need to think about: What user action should start which process? Should any code be executed immediately? With every update of a text field, with a click on a button, with "onfocus" or "onblur" etc.

    Finally you can implement the function that needs to run. Make sure that you test each step of your implementation to exclude "cascading errors".

  • Users who have thanked devnull69 for this post:

    newphpcoder (03-07-2012)

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    here is my html code:

    Code:
    <html>
    <head>
    <style type="text/css"> 
    #fieldset_PS{
        position: relative;
        width: 20%;
        height: auto;
        margin-left: .5em;
        top : 0px;
        
    }
    #main_button{
        margin-left: .7em;
        position: absolute;
        top : 0px;
    }
     
    #footer_button{
        margin-left: .5em;
        position: absolute;
        top : 535px;
    }
    </style>
    <link rel="stylesheet" type="text/css" href="kanban.css" /> 
    <script type="text/javascript"> 
      
    function display_PS(){
        document.loading_kanban.action="ParameterSettings.php";
        document.loading_kanban.submit();
    }
    function display_Kanban(){
        document.loading_kanban.action="kanban_report.php";
        document.loading_kanban.submit();
    }
    </script>
     
    </head>
     <form name="loading_kanban">
    <div id="main_button">
    <center>
    <!--<label style="margin-left: .9em; font-family: Arial, Helvetica, sans-serif; font-size: .7em;">Display Details:</label><input  onclick='showDetails(this);' id='chkDetail'   type='checkbox' checked='checked' value='wip'/>     -->
    <input type="button" name="parameter_settings" value="Parameter Settings" onclick="display_PS()">
    <input type="button" name="parameter_settings" value="Stock Requisition"> 
    <input type="button" name="parameter_settings" value="Kanban Report" onclick="display_Kanban()">
    </center>
    </div>
    <div id="fieldset_PS">
    <center>
    <table border="1">
    <th>Compounds</th>
    <th>Max</th>
    <th>Min</th>
    <tr>
            <td>P30</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P32</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P32W</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P35</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P35M</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P35W</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P38</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P41</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P42</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P43</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P45</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P46</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>P47</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr><tr>
            <td>TOTAL</td>
            <td><input type = 'text' name = 'max_pcode' size = '10'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr></form>
    </html>

  • #9
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Ok ... your HTML is weird already. You'll have to make sure that the same name will not be given to different fields (like max_pcode, min_pcode)

    So what about your program logic? Which fields do you want to accumulate? On which user action do you want to start the calculation? Does the calculation have to start on each field update? Where do you want to put the result? What happens if the user input is not numeric?

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Ok ... your HTML is weird already. You'll have to make sure that the same name will not be given to different fields (like max_pcode, min_pcode)

    So what about your program logic? Which fields do you want to accumulate? On which user action do you want to start the calculation? Does the calculation have to start on each field update? Where do you want to put the result? What happens if the user input is not numeric?
    the calculation will display on the last field which is total and when I input number on the first row it will automatically displayed on the total row and when I u=input number on the second row it will add on the total row.

    Thank you

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I tried this code:

    Code:
    <html>
    <head>
    <style type="text/css">
    #fieldset_PS{
        position: relative;
        width: 20%;
        height: auto;
        margin-left: .5em;
        top : 0px;
        
    }
    #main_button{
        margin-left: .7em;
        position: absolute;
        top : 0px;
    }
    
    #footer_button{
        margin-left: .5em;
        position: absolute;
        top : 560px;
    }
    
    </style>
    <link rel="stylesheet" type="text/css" href="kanban.css" /> 
    <script type="text/javascript">
    //=======display parameter settings==========//  
    function display_PS(){
        document.loading_kanban.action="ParameterSettings.php";
        document.loading_kanban.submit();
    }
    //=======display kanban=========//
    function display_Kanban(){
        document.loading_kanban.action="kanban_report.php";
        document.loading_kanban.submit();
    }
    
    //=======save param======//
    function save_param(){
        document.loading_kanban.action="ParameterSettingsSave.php";
        document.loading_kanban.submit();  
    }
    </script>
    
    <script type="text/javascript">
    //=========auto compute Total  Max=====//
    function autocalearn(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, Total_max = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    
    {
    field = arguments[a];
    val = parseFloat(field.value); //get value
    
    //if (!isNaN(val)) //number?
    if (!isNaN(val))
    {
    Total_max += val; //accumulate
    }
    }                                                                                 
    oForm.Total_max.value = Total_max.toFixed(2); //out
     
    }
    
    </script>
    
    
    <script type="text/javascript">
    //============Auto compute Total Min Lot========//
    function autocalmin(oText)
    {
    if (isNaN(oText.value)) //filter input
    {
    alert('Numbers only!');
    oText.value = '';
    }
    var field, val, oForm = oText.form, Total_min = a = 0;
    for (a; a < arguments.length; ++a) //loop through text elements
    {
    field = arguments[a];
    val = parseFloat(field.value); //get value
    if (!isNaN(val)) //number?
    {
    Total_min += val; //accumulate
    }
    }                                                 
    oForm.Total_min.value = Total_min.toFixed(2); //out
     
    }
    </script>
    
    </head>
     <?php
      error_reporting(0);
    $con = mysql_connect('localhost', 'root','');
    
    if (!$con) {
        echo 'failed';
        die();
    }
    
    mysql_select_db("mes", $con);
    
                                               
    ?>
    <body>
    <form name="loading_kanban">
    <div id="main_button">
    <center>
    <!--<label style="margin-left: .9em; font-family: Arial, Helvetica, sans-serif; font-size: .7em;">Display Details:</label><input  onclick='showDetails(this);' id='chkDetail'   type='checkbox' checked='checked' value='wip'/>     -->
    <input type="button" name="parameter_settings" value="Parameter Settings" onclick="display_PS()">
    <input type="button" name="parameter_settings" value="Stock Requisition"> 
    <input type="button" name="parameter_settings" value="Kanban Report" onclick="display_Kanban()">
    </center>
    </div>
    <div id="fieldset_PS">
    <center>
    <table border="1">
    <th>Compounds</th>
    <th>Max</th>
    <th>Min</th>
    
    <?php
    
    $sql = "SELECT PCODE FROM param_set ORDER BY PCODE ASC";
    $result = mysql_query($sql, $con);
    while ($row = mysql_fetch_assoc($result)){
    echo "<tr>
            <td>$row[PCODE]</td>
            <td><input type = 'text' name = 'max_pcode' size = '10' onkeyup='return autocalearn(this)'></td>
            <td><input type = 'text' name = 'min_pcode' size = '10'></td>
            </tr>"  ;
    }
    $sql = "SELECT Total FROM param_total";	
    $res = mysql_query($sql, $con);
    while ($row1 = mysql_fetch_assoc($res)){
    echo "<tr>
    <td>$row1[Total]</td>
    <td><input type = 'text' name = 'Total_max' id='Total_max' size = '10'></td>
    <td><input type = 'text' name = 'Total_min' id='Total_min' size = '10'></td>
    ";	 
    }    
    echo "<input type='button' name='save' id='save' value='save' onclick='param_save()'>"
    ?>
    </form>
    </body>
    </html>
    the problem is it did not sum the data that I was inputted in a textbox..

    I think the problem is in my function.
    I don't know how can i revise my function to make it work.

    Thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •