0

how can I use the value entered in total_no_of_units and cost_per_unit in total_cost_acquistion i.e(total cost =units* cost per unit) pls suggest me any way to do it..the value value of total_cost_acquistion must be displaye as soon as the no_of_units and cost_per_units are given....

<?php
    require_once "session.php";
?>
<?php
    include('connection.php');
    if (isset($_POST['submit'])){
      $sl_no =$_POST['sl_no'];
      $dec =$_POST['description'];
      $loctn =$_POST['location'];
      $ref_no =$_POST['reference_no'];
      $d_of_aqstn =$_POST['date_of_acquisation'];
      $t_of_un =$_POST['total_no_of_units'];
      $cst_p_un =$_POST['cost_per_unit'];
      $tl_cost_aqstn =$_POST['total_cost_of_acquistion'];
      $lf_ast_pr_cdl =$_POST['life_of_assets_per_codal'];
      $no_yr_ast_use =$_POST['no_year_assets_use'];
      $rt_deprtn =$_POST['rate_depreciation'];
      $act_deprtn =$_POST['accumulate_depreciation'];
      $val_as_on = $_POST['value_as_on'];
      $remks =$_POST['remarks'];

      $query = "insert into form(sl_no, description, location, reference_no, date_of_acquisation, total_no_of_units, cost_per_unit, total_cost_of_acquistion, life_of_assets_per_codal, no_year_assets_use, rate_depreciation, accumulate_depreciation, value_as_on, remarks)
     values('$sl_no','$dec','$loctn','$ref_no','$d_of_aqstn','$t_of_un','$cst_p_un','$tl_cost_aqstn','$lf_ast_pr_cdl','$no_yr_ast_use','$rt_deprtn','$act_deprtn','$val_as_on','$remks')";
     $retval = mysql_query($query,$con);
     if(!$retval )
       {
        die('Could not enter data: ' . mysql_error());
       }
     mysql_close($con);
    }
  ?>
  <html>
  <head>
       <meta charset = "utf-8">
       <link rel = "stylesheet"
            href = "//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

         <script type = "text/javascript"
            src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

         <script type = "text/javascript"
            src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>

         <script>
            $(function() {
               $( "#datepicker" ).datepicker();
            });
         </script>
       <title>Add a Form</title>
     </head>
      <body>
    <h1 align="center"><strong>FIXED ASSET REGISTER</strong></h1>
    <b align="center">Welcome : <i><?php echo $_SESSION['login_user']; ?> </i></b>
      <br>

        <form method="POST" action="<?php $PHP_SELF ?>">
        <div align="center">
            <table width=100%  cellpadding="5" cellspacing="2" align="center">
              <tr>
                <td colspan="2" align="center"> <h2><strong>FORM-10</strong></h2></td>
              </tr>
              <tr>
                <td align="right"> <strong>Serial_No:</strong></td>
                <td><input type="text"  name="sl_no"  placeholder="Serial_no" required></td>
              </tr>

              <tr>
                <td align="right"> <strong>Description :</strong></td>
                <td><input type="text"  name="description"  placeholder="description" required></td>
              </tr>
              <tr>
                <td align="right"> <strong>Location:</strong></td>
                <td><input type="text" name="location"  placeholder="location" required></td>
              </tr>
              <tr>
                <td align="right"> <strong>Reference_no:</strong></td>
                <td><input type="text"  name="reference_no"  placeholder="reference_no" required></td>
              </tr>

             <tr>
                <td align="right"> <strong>Date_of_Acquisition:</strong></td>
                <td><input type="text"  name="date_of_acquisation" id = "datepicker" placeholder="date_of_acquisation" required></td>
              </tr>

                <tr>

              <td align="right"> <strong>Total_no_of_Units:</strong></td>
                <td><input type="text"  name="total_no_of_units"  placeholder="total_no_of_units" required></td>

              </tr>

                <tr>
              <td align="right"> <strong>Cost per Unit:</strong></td>
                <td><input type="text" name="cost_per_unit" placeholder="cost_per_unit" required></td>

              </tr>

                <tr>

              <td align="right"> <strong>Total Acquisition Cost:</strong></td>
                <td><input type="text" name="total_cost_of_acquistion" placeholder="total_cost_of_acquistion" required></td>


              </tr>

                <tr>
                        <td align="right"> <strong>Asset Life:</strong></td>
                <td><input type="text" name="life_of_assets_per_codal" placeholder="life_of_assets_per_codal" required></td>
                </tr>

                <tr>
                        <td align="right"> <strong>Years asset used:</strong></td>
                <td><input type="text" name="no_year_assets_use" placeholder="no_of_year_assets_use" required></td>
                </tr>

                <tr>
                        <td align="right"> <strong>Depriciation Rate:</strong></td>
                <td><input type="text" name="rate_depreciation" placeholder="rate_depreciation" required></td>
                </tr>

                <tr>
                        <td align="right"> <strong>Accumulated Depriciation:</strong></td>
                <td><input type="text" name="accumulate_depreciation" placeholder="accumulate_depreciation_till_31/03/2016" required></td>
                </tr>

                <tr>
                        <td align="right"> <strong>Valuation:</strong></td>
                <td><input type="text" name="value_as_on" placeholder="value_as_on_31/03/2016" required></td>
                </tr>

                <tr>
                        <td align="right"> <strong>Remarks:</strong></td>
                <td><input type="text" name="remarks" placeholder="remarks" ></td>
        </tr>




            <!--  <tr>
                <td align="right"> <strong>Status: </strong></td>
                <td><select name="status" required>
                    <option></option>
                    <option>New</option>
                    <option>Old</option>
                    <option>Lost</option>
                    <option>Damage</option>
                    <option>Subject for Replacement</option>
                    <option>Archive</option>
                </select>
                </td>
            </tr>-->
              <tr>
                <td colspan="2" align="center"><input type='submit' value=' Save ' name='submit'>

              </tr>
            </table>
            <h4 align="right"><a href="mainpage1.php">BACK</a></h4>
        </div>
         </form>

    </body>
    </html>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • Please try to consolidate your answer. It is a little hard to figure out what you want when you post a block of code. Isolate the problem and it will be much easier to answer. – JoshKopen Jun 16 '17 at 15:52
  • it's unclear what you are asking to me... What's wrong with "total cost =units* cost" ? – Jeff Jun 16 '17 at 15:53
  • 2
    No information here... just commenting on the fact I find it really weird when coders do this `?> – Dale Jun 16 '17 at 15:53
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 16 '17 at 15:55
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 16 '17 at 15:55
  • is there any way so that when ever i enter the value of total no of units in( (total_no_of_units) and the cost per unit of any asset in(cost_per_unit) the total accumulated cost i.e(total_no_of_units* cost_per_unit) will be displayed in total_cost_acquistion – Akhil Jaiswal Jun 16 '17 at 15:59
  • PHP, JS, and mysql all offer math functions `num * num `. Where are you wanting to show the calculation? – chris85 Jun 16 '17 at 16:00
  • in total_cost_of_acquistion – Akhil Jaiswal Jun 16 '17 at 16:01

4 Answers4

0

I think this is what you want:

<?php
require_once "session.php";
?>
<?php
include('connection.php');
if (isset($_POST['submit'])){
$sl_no =$_POST['sl_no'];
$dec =$_POST['description'];
$loctn =$_POST['location'];
$ref_no =$_POST['reference_no'];
$d_of_aqstn =$_POST['date_of_acquisation'];
$t_of_un =$_POST['total_no_of_units'];
$cst_p_un =$_POST['cost_per_unit'];
$tl_cost_aqstn =$_POST['total_cost_of_acquistion'];
$lf_ast_pr_cdl =$_POST['life_of_assets_per_codal'];
$no_yr_ast_use =$_POST['no_year_assets_use'];
$rt_deprtn =$_POST['rate_depreciation'];
$act_deprtn =$_POST['accumulate_depreciation'];
$val_as_on = $_POST['value_as_on'];
$remks =$_POST['remarks'];

$query = "insert into form(sl_no, description, location, reference_no, date_of_acquisation, total_no_of_units, cost_per_unit, total_cost_of_acquistion, life_of_assets_per_codal, no_year_assets_use, rate_depreciation, accumulate_depreciation, value_as_on, remarks)
 values('$sl_no','$dec','$loctn','$ref_no','$d_of_aqstn','$t_of_un','$cst_p_un','$tl_cost_aqstn','$lf_ast_pr_cdl','$no_yr_ast_use','$rt_deprtn','$act_deprtn','$val_as_on','$remks')";
 $retval = mysql_query($query,$con);
 if(!$retval )
   {
    die('Could not enter data: ' . mysql_error());
   }
 mysql_close($con);
}
  ?>
  <html>
 <head>
   <meta charset = "utf-8">
   <link rel = "stylesheet"
        href = "//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

     <script type = "text/javascript"
        src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

     <script type = "text/javascript"
        src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>

     <script>
        $(function() {
           $( "#datepicker" ).datepicker();
        });
     </script>
   <title>Add a Form</title>
 </head>
  <body>
<h1 align="center"><strong>FIXED ASSET REGISTER</strong></h1>
<b align="center">Welcome : <i><?php echo $_SESSION['login_user']; ?> </i></b>
  <br>

    <form method="POST" action="<?php $PHP_SELF ?>">
    <div align="center">
        <table width=100%  cellpadding="5" cellspacing="2" align="center">
          <tr>
            <td colspan="2" align="center"> <h2><strong>FORM-10</strong></h2></td>
          </tr>
          <tr>
            <td align="right"> <strong>Serial_No:</strong></td>
            <td><input type="text"  name="sl_no"  placeholder="Serial_no" required></td>
          </tr>

          <tr>
            <td align="right"> <strong>Description :</strong></td>
            <td><input type="text"  name="description"  placeholder="description" required></td>
          </tr>
          <tr>
            <td align="right"> <strong>Location:</strong></td>
            <td><input type="text" name="location"  placeholder="location" required></td>
          </tr>
          <tr>
            <td align="right"> <strong>Reference_no:</strong></td>
            <td><input type="text"  name="reference_no"  placeholder="reference_no" required></td>
          </tr>

         <tr>
            <td align="right"> <strong>Date_of_Acquisition:</strong></td>
            <td><input type="text"  name="date_of_acquisation" id = "datepicker" placeholder="date_of_acquisation" required></td>
          </tr>

            <tr>

          <td align="right"> <strong>Total_no_of_Units:</strong></td>
            <td><input type="text"  name="total_no_of_units" id="total_no_of_units" onkeyup="calculate()" placeholder="total_no_of_units" required></td>

          </tr>

            <tr>
          <td align="right"> <strong>Cost per Unit:</strong></td>
            <td><input type="text" name="cost_per_unit" id="cost_per_unit" onkeyup="calculate()" placeholder="cost_per_unit" required></td>

          </tr>

            <tr>

          <td align="right"> <strong>Total Acquisition Cost:</strong></td>
            <td><input type="text" name="total_cost_of_acquistion" id="total_cost_of_acquistion" placeholder="total_cost_of_acquistion" required></td>


          </tr>

            <tr>
                    <td align="right"> <strong>Asset Life:</strong></td>
            <td><input type="text" name="life_of_assets_per_codal" placeholder="life_of_assets_per_codal" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Years asset used:</strong></td>
            <td><input type="text" name="no_year_assets_use" placeholder="no_of_year_assets_use" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Depriciation Rate:</strong></td>
            <td><input type="text" name="rate_depreciation" placeholder="rate_depreciation" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Accumulated Depriciation:</strong></td>
            <td><input type="text" name="accumulate_depreciation" placeholder="accumulate_depreciation_till_31/03/2016" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Valuation:</strong></td>
            <td><input type="text" name="value_as_on" placeholder="value_as_on_31/03/2016" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Remarks:</strong></td>
            <td><input type="text" name="remarks" placeholder="remarks" ></td>
    </tr>




        <!--  <tr>
            <td align="right"> <strong>Status: </strong></td>
            <td><select name="status" required>
                <option></option>
                <option>New</option>
                <option>Old</option>
                <option>Lost</option>
                <option>Damage</option>
                <option>Subject for Replacement</option>
                <option>Archive</option>
            </select>
            </td>
        </tr>-->
          <tr>
            <td colspan="2" align="center"><input type='submit' value=' Save ' name='submit'>

          </tr>
        </table>
        <h4 align="right"><a href="mainpage1.php">BACK</a></h4>
    </div>
     </form>
<script>
    function calculate(){
        var number = $('#total_no_of_units').val();
        var unitCost = $('#cost_per_unit').val();
        var totalCost = $('#total_cost_of_acquistion').val();
        if(number=='' || unitCost==''){
            $('#total_cost_of_acquistion').val('0');
            return false;
        }
        else{
            $('#total_cost_of_acquistion').val(number*unitCost);
        }
    }
</script>
</body>
</html>
Echoes
  • 324
  • 4
  • 14
0

If you're talking about live updating one field as the value of another field changes, then you will have to use client-side script like Javascript, Node, Jquery, etc. I recommend setting a default value for all fields, that way you don't have to check if the value is empty or not.

<input id='field1' type='text' value='0' />
<input id='field2' type='text' value='0' />
<input id='answer' type='text' value='0' />

<script>
  $('#field1').on('input', function(e){
    $('#answer').val(parseInt($(this).val()) * parseInt($('#field2').val()));
  });
  $('#field2').on('input', function(e){
    $('#answer').val(parseInt($(this).val()) * parseInt($('#field1').val()));
  });
</script>

You can sub out the correct fields and change the math as needed, but the concept is the same.

If you're only making the change after submitting the data, then it's just simply echoing out the value from the server-side code to the client-side code in the appropriate value fields.

I hope this answers your question.

0

You can use jquery to display total_cost_acquisition on user events.

something like,

$( document ).ready(function() {

   $('[name="cost_per_unit"]').keyup(function() {
     calculateTotal();
   });

   $('[name="total_no_of_units"]').keyup(function() {
     calculateTotal();
   });
});

function calculateTotal(){
    var total = ($('[name="cost_per_unit"]').val() * $('[name="total_no_of_units"]').val());
    $('[name="total_cost_of_acquistion"]').val(total);
}

Hope it would solve your problems

Josmy Jose
  • 36
  • 3
0

Try the following code, notice the changes I've made to the script in the commented lines:

    <?php
require_once "session.php";
?>
<?php
include('connection.php');
if (isset($_POST['submit'])){
$sl_no =$_POST['sl_no'];
$dec =$_POST['description'];
$loctn =$_POST['location'];
$ref_no =$_POST['reference_no'];
$d_of_aqstn =$_POST['date_of_acquisation'];
$t_of_un =$_POST['total_no_of_units'];
$cst_p_un =$_POST['cost_per_unit'];
$tl_cost_aqstn =$_POST['total_cost_of_acquistion'];
$lf_ast_pr_cdl =$_POST['life_of_assets_per_codal'];
$no_yr_ast_use =$_POST['no_year_assets_use'];
$rt_deprtn =$_POST['rate_depreciation'];
$act_deprtn =$_POST['accumulate_depreciation'];
$val_as_on = $_POST['value_as_on'];
$remks =$_POST['remarks'];

$query = "insert into form(sl_no, description, location, reference_no, date_of_acquisation, total_no_of_units, cost_per_unit, total_cost_of_acquistion, life_of_assets_per_codal, no_year_assets_use, rate_depreciation, accumulate_depreciation, value_as_on, remarks)
 values('$sl_no','$dec','$loctn','$ref_no','$d_of_aqstn','$t_of_un','$cst_p_un','$tl_cost_aqstn','$lf_ast_pr_cdl','$no_yr_ast_use','$rt_deprtn','$act_deprtn','$val_as_on','$remks')";
 $retval = mysql_query($query,$con);
 if(!$retval )
   {
    die('Could not enter data: ' . mysql_error());
   }
 mysql_close($con);
}
  ?>
  <html>
 <head>
   <meta charset = "utf-8">
   <link rel = "stylesheet"
        href = "//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

     <script type = "text/javascript"
        src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

     <script type = "text/javascript"
        src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>

     <script>
        $(function() {
           $( "#datepicker" ).datepicker();
        });
        //Added a Script to Populate the Fields
        $(document).on('change','#total_no_of_units',function() {
            var AmountOfUnits = $(this).val();
            if($("#cost_per_unit").val() === '') {
                //If there is no value in cost_per_unit do not calculate a total
            }//end if
            else {
                //cost_per_unit is not empty and we'll calculate a total value
                var CostPerUnit = $("#cost_per_unit").val();
                //Set the value of total_cost_of_acquistion
                var TotalCost = AmountOfUnits * CostPerUnit;
                $("#total_cost_of_acquisition").val(TotalCost);
            }//end else
        });//end on change
        $(document).on('change','#cost_per_unit',function() {
            var CostPerUnit = $(this).val();
            if($("#total_no_of_units").val() === '') {
                //If there is no value in total_no_of_units do not calculate a total
            }//end if
            else {
                //total_no_of_units is not empty and we'll calculate a total value
                var AmountOfUnits = $("#total_no_of_units").val();
                //Set the value of total_cost_of_acquistion
                var TotalCost = AmountOfUnits * CostPerUnit;
                $("#total_cost_of_acquisition").val(TotalCost);
            }//end else
        });//end on change                  
     </script>
   <title>Add a Form</title>
 </head>
  <body>
<h1 align="center"><strong>FIXED ASSET REGISTER</strong></h1>
<b align="center">Welcome : <i><?php echo $_SESSION['login_user']; ?> </i></b>
  <br>

    <form method="POST" action="<?php $PHP_SELF ?>">
    <div align="center">
        <table width=100%  cellpadding="5" cellspacing="2" align="center">
          <tr>
            <td colspan="2" align="center"> <h2><strong>FORM-10</strong></h2></td>
          </tr>
          <tr>
            <td align="right"> <strong>Serial_No:</strong></td>
            <td><input type="text"  name="sl_no"  placeholder="Serial_no" required></td>
          </tr>

          <tr>
            <td align="right"> <strong>Description :</strong></td>
            <td><input type="text"  name="description"  placeholder="description" required></td>
          </tr>
          <tr>
            <td align="right"> <strong>Location:</strong></td>
            <td><input type="text" name="location"  placeholder="location" required></td>
          </tr>
          <tr>
            <td align="right"> <strong>Reference_no:</strong></td>
            <td><input type="text"  name="reference_no"  placeholder="reference_no" required></td>
          </tr>

         <tr>
            <td align="right"> <strong>Date_of_Acquisition:</strong></td>
            <td><input type="text"  name="date_of_acquisation" id = "datepicker" placeholder="date_of_acquisation" required></td>
          </tr>

            <tr>

          <td align="right"> <strong>Total_no_of_Units:</strong></td>
            <!-- Added an Id to the form element so I can select it with JavaScript -->
            <td><input type="text"  id="total_no_of_units" name="total_no_of_units"  placeholder="total_no_of_units" required></td>

          </tr>

            <tr>
          <td align="right"> <strong>Cost per Unit:</strong></td>
            <!-- Added an Id to the form element so I can select it with JavaScript -->
            <td><input type="text" id="cost_per_unit" name="cost_per_unit" placeholder="cost_per_unit" required></td>

          </tr>

            <tr>

          <td align="right"> <strong>Total Acquisition Cost:</strong></td>
            <!-- Added an Id to the form element so I can select it with JavaScript -->
            <td><input type="text" id="total_cost_of_acquisition" name="total_cost_of_acquistion" placeholder="total_cost_of_acquistion" required></td>


          </tr>

            <tr>
                    <td align="right"> <strong>Asset Life:</strong></td>
            <td><input type="text" name="life_of_assets_per_codal" placeholder="life_of_assets_per_codal" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Years asset used:</strong></td>
            <td><input type="text" name="no_year_assets_use" placeholder="no_of_year_assets_use" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Depriciation Rate:</strong></td>
            <td><input type="text" name="rate_depreciation" placeholder="rate_depreciation" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Accumulated Depriciation:</strong></td>
            <td><input type="text" name="accumulate_depreciation" placeholder="accumulate_depreciation_till_31/03/2016" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Valuation:</strong></td>
            <td><input type="text" name="value_as_on" placeholder="value_as_on_31/03/2016" required></td>
            </tr>

            <tr>
                    <td align="right"> <strong>Remarks:</strong></td>
            <td><input type="text" name="remarks" placeholder="remarks" ></td>
    </tr>




        <!--  <tr>
            <td align="right"> <strong>Status: </strong></td>
            <td><select name="status" required>
                <option></option>
                <option>New</option>
                <option>Old</option>
                <option>Lost</option>
                <option>Damage</option>
                <option>Subject for Replacement</option>
                <option>Archive</option>
            </select>
            </td>
        </tr>-->
          <tr>
            <td colspan="2" align="center"><input type='submit' value=' Save ' name='submit'>

          </tr>
        </table>
        <h4 align="right"><a href="mainpage1.php">BACK</a></h4>
    </div>
     </form>

</body>
</html>
Julian Koster
  • 262
  • 1
  • 10