【问题标题】:get addition without button click无需单击按钮即可添加
【发布时间】:2017-09-29 15:09:26
【问题描述】:

任何人都可以帮助我获得基本工资和津贴的增加,并将其插入到总日薪中,而无需单击按钮。这是我的 php 代码。
这是我的 UI 的 Screen Shot
代码

<?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);
?>      

在我输入基本工资和津贴 1 的值后,我想自动将这两个加到 Day Rate 中。
这是我的 HTML 代码。

<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>

【问题讨论】:

  • 嗨,我不明白你的问题,你现在在做什么?你想达到什么目标?
  • 使用 Javascript/Jquery 从两个字段中获取值并将两个值的总和放入 Total Day Rate 字段中。这很简单。试着弄脏你的手。
  • 也添加您的 HTML 代码,您需要 javascript/jQuery 为您完成。
  • 在基本工资和津贴 1 中输入值后,我想在总天数框中自动添加这两者。
  • get the addition of Basic Salary and Allowance I and insert it in Total Day Rate.... 像这样:` $result = $BasicSalary + $Allowance1;` $query = mysql_query("insert into attendance (DayRate) values ('$result')"); if($query!=false){ echo something about the success }else{ mysql errno etc... } 老实说,您可以在不刷新的情况下处理 php页面,因此您可能正在寻找一个 angular/JS/JQuery 方法来将值推送到您的数组中,然后再使用 post 方法提交它。

标签: php html mysql database


【解决方案1】:

这就是使用提交 post 的方式,因为这些都是 _POST 值...虽然在不按按钮的情况下完成此操作将是 JS/Angular/J Query/AJAX...

....
$BasicSalary = $_POST['BasicSalary']; 
$Allowance1 = $_POST['Allowance1'];
$Allowance2 = $_POST['Allowance2'];
$DayRate = $_POST['$DayRate'];
$OTrate = $_POST['OTrate'];

//Set a new variable with the addition of the two `Basic Salary` and  `Allowance 1` 
//for the insertion into your column `dayRate`   
$adustedDayRate = $BasicSalary + $Allowance1;
if($ID !=''||$Name !=''){

    if($query != false){
        $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','$adustedDayRate','$OTrate')");
    echo "<br/><br/><span>Data Inserted successfully...!!</span>";
    }esle{ $_SESSION['err'] = "ERROR: ".--- MySQL error handle here --- }
}
else{
echo "Some Fields are Blank....!!</p>";   
}

}

mysql_close($connection);

使用 Angular,您可以在表单输入中为 `DayRate 放置一个占位符元素,然后为要添加的两个输入添加 ng-model 元素的回调。这里是这样的:

<div ng-app="">

<p>BasicSalary : <input type="number" ng-model="BasicSalary" placeholder="Basic Salary"></p>

<p>AllowanceI  : <input type="number" ng-model="AllowanceI" placeholder="Allowance I"></p>

<p>DayRate   : <input type="number" ng-model="DayRate" placeholder="{{BasicSalary -- AllowanceI}}"></p>

您的代码如下所示:

<fieldset> Basic Salary:
 <input class="input" type="number" ng-model="BasicSalary" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" ng-model="Allowance1" 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" placeholder="{{ BasicSalary -- Allowance1 }}" value="" />
</fieldset>

这里是 Angular 方法的一个工作小提琴,只需使用 Composer 或托管链接将 Angular 库添加到您的服务器,Angular 库元素不需要额外的 JS。您可以在输入其他两个组合输入时更改 DayRate 的值。 https://jsfiddle.net/qkpfb90o/1/

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    据我了解您的问题,您的 HTML 代码应该是这样的,

    <!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>
    

    我已经添加了 jQuery,它可以完成你的工作,你不需要在 PHP 端进行添加。

    【讨论】:

    • 当您更改BasicSalaryAllowance1Allowance2时,它会自动计算这三者的总和并将其放入DayRate.
    【解决方案3】:

    你也可以试试这个。

    <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="" id="BasicSalary" onkeyup="doSum()"/>
      </fieldset>
      <fieldset> Allowance I:
          <input class="input" type="number" name="Allowance1" value="" id="Allowance1" onkeyup="doSum()"/>
      </fieldset>
      <fieldset>Allowance II:
          <input class="input" type="number" name="Allowance2"  />
      </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>
    

    <script>
    function doSum() {
        var Allowance1 = isNaN(document.getElementById('Allowance1').value) ? document.getElementById('Allowance1').value : 0;
        var BasicSalary = isNaN(document.getElementById('BasicSalary').value) ? document.getElementById('BasicSalary').value : 0;
        var tot = parseInt(Allowance1) + parseInt(BasicSalary);
        document.getElementById('DayRate').value = tot;
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2015-04-15
      • 2020-12-31
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多