Skip to content
Advertisement

Add Sum Of Two Input Values Automatically without clicking a single button

I have tried all codes avaliable but nothing worked. Here is the Code :

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    
<script>
var text1 = document.getElementById("inhouse");
var text2 = document.getElementById("sahodaya");

function showsum() {
   var first_number = parseFloat(text1.value);
   if (isNaN(first_number)) first_number = 0;
   var second_number = parseFloat(text2.value);
   if (isNaN(second_number)) second_number = 0;
   var result = first_number + second_number;
   document.getElementById("total").value = result;
}
</script>
    <title>Sahodaya 25 Report - Form</title>
  </head>
  <body>
    <div class = "container my-3">
        <form method="POST">
<fieldset disabled>
    <div class="mb-3">
      <label for="disabledTextInput" class="form-label">Name</label>
      <input type="text" id="disabledTextInput" class="form-control" placeholder="<?php echo $_SESSION['name']?>" name = "name" value="<?php echo $_SESSION['name']?>">
      
      
      <label for="disabledTextInput2" class="form-label my-2">Email</label>
      <input type="text" id="disabledTextInput2" class="form-control" placeholder="<?php echo $_SESSION['email']?>" name = "email" value="<?php echo $_SESSION['email']?>">
      
      </fieldset>
      <label for="inhouse" class="form-label">Inhouse Training Done In Hours</label>
      <input type="number" id="inhouse" class="form-control" placeholder="Type Here" name = "inhouse" required oninput="showsum()"><br>
      
      <label for="sahodaya" class="form-label">Sahodaya Training Done In Hours</label>
      <input type="number" id="sahodaya" class="form-control" placeholder="JSSC + PSCC (Both)" name = "sahodaya" oninput="showsum()" required><br>
      
      <label for="total" class="form-label">Total Hours Done</label>
      <input type="number" id="total" class="form-control" name = "total" value="0"><br>
      
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
    </div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
  </body>
</html>

But this Code is Not Wokring I tried all Solutions Available on Stack Overflow that I seen till now but nothing helped much So i m posting this if somebody can help.

I want to add the value in inhouse and sahodaya and show it in total input field.

But when I m running this it’s not wokring. How can i fix this Error.

Advertisement

Answer

Try the below code. It should fix the issue.

Full working code snippet:

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <script>
    function add_number() {
      var first_number = parseFloat(document.getElementById("inhouse").value);
      if (isNaN(first_number)) first_number = 0;
      var second_number = parseFloat(document.getElementById("sahodaya").value);
      if (isNaN(second_number)) second_number = 0;
      var result = first_number + second_number;
      document.getElementById("total").value = result;
    }
  </script>
  <title>Sahodaya 25 Report - Form</title>
</head>

<body>
  <div class="container my-3">
    <form method="POST">
      <fieldset disabled>
        <div class="mb-3">
          <label for="disabledTextInput" class="form-label">Name</label>
          <input type="text" id="disabledTextInput" class="form-control" placeholder="<?php echo $_SESSION['name']?>" name="name" value="<?php echo $_SESSION['name']?>">


          <label for="disabledTextInput2" class="form-label my-2">Email</label>
          <input type="text" id="disabledTextInput2" class="form-control" placeholder="<?php echo $_SESSION['email']?>" name="email" value="<?php echo $_SESSION['email']?>">

      </fieldset>
      <label for="inhouse" class="form-label">Inhouse Training Done In Hours</label>
      <input type="number" id="inhouse" class="form-control" placeholder="Type Here" name="inhouse" required onkeyup="add_number()"><br>

      <label for="sahodaya" class="form-label">Sahodaya Training Done In Hours</label>
      <input type="number" id="sahodaya" class="form-control" placeholder="JSSC + PSCC (Both)" name="sahodaya" onkeyup="add_number()" required><br>

      <label for="total" class="form-label">Total Hours Done</label>
      <input type="number" id="total" class="form-control" name="total" value="0"><br>

      <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    </div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
</body>

</html>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement