duangongqiong6958 2017-05-02 07:05
浏览 10
已采纳

无按钮点击即可获得添加

Can anyone help me to get the addition of Basic Salary and Allowance I and insert it in Total Day Rate without clicking a button. here's my php code.
Here's a Screen Shot of my UI.
Code :

<?php

    $connection = mysql_connect("localhost", "root", "");

        $db = mysql_select_db("laboursalary", $connection);
        if(isset($_POST['submit'])){


    $ID = $_POST['ID'];
    $Name = $_POST['Name'];
    $Location = $_POST['Location'];
    $Category = $_POST['Category'];
    $LabourSupplier = $_POST['LabourSupplier'];
    $Home = $_POST['Home'];
    $Mobile = $_POST['Mobile'];
    $BasicSalary = $_POST['BasicSalary'];
    $Allowance1 = $_POST['Allowance1'];
    $Allowance2 = $_POST['Allowance2'];
    $DayRate = $_POST['$DayRate'];
    $OTrate = $_POST['OTrate'];
    if($ID !=''||$Name !=''){

    $query = mysql_query("insert into attendance(ID, Name, Location, Category,LabourSupplier,Home,Mobile,BasicSalary,Allowance1,Allowance2,DayRate,OTrate) values ('$ID','$Name','$Location','$Category','$LabourSupplier','$Home','$Mobile','$BasicSalary','$Allowance1','$Allowance2','$DayRate','$OTrate')");
    echo "<br/><br/><span>Data Inserted successfully...!!</span>";
    }
    else{
    echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";   
    }

    }

    mysql_close($connection);
?>      

After I enter the values for Basic Salary and Allowance 1, I want to get the addition of those two in Day Rate automatically.
this is my HTML code.

<form id="details" action="" method="POST">

    <fieldset> ID:
      <input class="input" type="text" name="ID" value="" />
      </fieldset>
    <fieldset> Name:
      <input class="input" type="text" name="Name" value="" />
    </fieldset>
    <fieldset> Location:
      <input class="input" type="text" name="Location" value="" />
    </fieldset>
    <fieldset> Category:
      <input class="input" type="text" name="Category" value="" />
    </fieldset>
    <fieldset> Labour Supplier:
     <input class="input" type="text" name="LabourSupplier" value="" />
    </fieldset>
    <fieldset> Telephone:
     <input class="input" type="text" name="Home" value="" />
    </fieldset>
    <fieldset>Mobile:
      <input class="input" type="text" name="Mobile" value="" />
    </fieldset>
    <fieldset> Basic Salary:
     <input class="input" type="number" name="BasicSalary" value="" />
    </fieldset>
    <fieldset> Allowance I:
    <input class="input" type="number" name="Allowance1" value="" />
    </fieldset>
    <fieldset>Allowance II:
     <input class="input" type="number" name="Allowance2" value="" />
    </fieldset>
    <fieldset>Total Day Rate:
     <input class="input" type="number" name="DayRate" value="" />
    </fieldset>
    <fieldset>OT Rate:
     <input class="input" type="number" name="OTrate" value="" />
    </fieldset>
    <fieldset>
      <button name="submit" type="submit" id="submit">Insert</button>
      <button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
    </fieldset>

  </form>
  • 写回答

3条回答 默认 最新

  • doukuangxiu1621 2017-05-02 08:27
    关注

    As I understood your question, your HTML code should be like,

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
    <form id="details" action="" method="POST">
        <fieldset> ID:
          <input class="input" type="text" name="ID" value="" />
          </fieldset>
        <fieldset> Name:
          <input class="input" type="text" name="Name" value="" />
        </fieldset>
        <fieldset> Location:
          <input class="input" type="text" name="Location" value="" />
        </fieldset>
        <fieldset> Category:
          <input class="input" type="text" name="Category" value="" />
        </fieldset>
        <fieldset> Labour Supplier:
         <input class="input" type="text" name="LabourSupplier" value="" />
        </fieldset>
        <fieldset> Telephone:
         <input class="input" type="text" name="Home" value="" />
        </fieldset>
        <fieldset>Mobile:
          <input class="input" type="text" name="Mobile" value="" />
        </fieldset>
        <fieldset> Basic Salary:
         <input class="input" type="number" name="BasicSalary" value="0" id="bassal" />
        </fieldset>
        <fieldset> Allowance I:
        <input class="input" type="number" name="Allowance1" value="0" id="all1" />
        </fieldset>
        <fieldset>Allowance II:
         <input class="input" type="number" name="Allowance2" value="0" id="all2" />
        </fieldset>
        <fieldset>Total Day Rate:
         <input class="input" type="number" name="DayRate" value="" id="DayRate" />
        </fieldset>
        <fieldset>OT Rate:
         <input class="input" type="number" name="OTrate" value="" />
        </fieldset>
        <fieldset>
          <button name="submit" type="submit" id="submit">Insert</button>
          <button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
        </fieldset>
      </form>
    <script >
        $(document).ready(function (){
            $('#bassal').on('input', function() {
                $('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
            });
            $('#all1').on('input', function() {
                $('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
            });
            $('#all2').on('input', function() {
                $('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
            });
        });
    </script>
    

    I've added jQuery, that does your work, and you don't need to do addition on PHP side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了