如何使用jquery在下一个字段中加总和减少值?

时间:2017-04-21 04:04:50

标签: php jquery mysql

我有两个输入和两个id作为属性,如何将第一个id与数据库中的现有数据相加,结果将随着id的下一个减少?

<input type="text" name="salary" id="salary" readonly value="5000000">

<!-- This is an input 1 to sum value -->
<input type="text" name="income" id="income">

<!-- The result -->
<input type="text" name="tot_salary" id="tot_salary" readonly value="The Results...">

<!-- This is an input to cut down or decrease result -->
<input type="text" name="pieces" id="pieces">

在javascript中:

<script>
$(document).ready(function() {

$("#select").change(function(e) {
    e.preventDefault();

    var EmpID = $(this).val();

    $.ajax({

        url: "http://localhost/sim/adm/salary.php",
        type: "GET",
        dataType: "json",
        data: {salary: idEmployee},
        success: function (result) {

            if (result.KelID == 4) {
                $("#tot_salary").attr("style","display: inline");
            }else{
                $("#tot_salary").attr("style","display: none");
            }   

            var RoleName = $("#role_name").val(result.RoleName);
            var Salary = $("#salary").val(result.EmpSalary);
            var Absen = $("#absen").val(result.Absen);
            var Bonus = $("#bonus").val(result.Bonus);
            var BaSalary = $("#BaSalary").val(result.BaSalary);
            var RoleID = $("#role_id").val(result.RoleID);

        }
    });

});

function toRp(value){
    var rev     = parseInt(value, 10).toString().split("").reverse().join("");
    var rev2    = "";
    for(var i = 0; i < rev.length; i++){
        rev2  += rev[i];
        if((i + 1) % 3 === 0 && i !== (rev.length - 1)){
            rev2 += ".";
        }
    }
    return "Rp. " + rev2.split("").reverse().join("") + ",00-";
}


$("#total_salary").keyup(function(){

    var salary = parseInt($("#salary").val()) || 0;
    var total_salary = parseInt($("#tot_salary").val()) || 0;

    var total = (parseInt(salary) + parseInt(tot_salary));

    $("#pieces").val(total.toFixed(2));

    // $("#pieces").html(total - pieces);
   });
});

在php中:

<?php 

 header('Content-type: application/json');

 $host = 'localhost';
 $user = 'root';
 $pass = '';
 $dbms = 'sim';

 $conn = new MySQLi($host,$user,$pass,$dbms);

  if (isset($_GET['gaji'])) {

   $data = $_GET['gaji'];

  // $data = 'PG-1701-001';

   $sql = $conn->query("
SELECT a.masuk AS MASUK
     , a.id_peg AS ID_PEG
     , p.nm_peg AS NM_PEG
     , k.nm_kel AS NM_KEL
     , k.kel_id AS KEL_ID 
  FROM absensi a
  JOIN dt_pegawai p
    ON a.id_peg = p.id_peg 
  JOIN kel_pengguna k
    ON p.kel_id = k.kel_id 
 WHERE p.id_peg = '$data'
");

   $res = $sql->fetch_assoc();

    $gaji = ($res['KEL_ID'] == 4) ?  $res['MASUK'] * 25000 :  $res['MASUK'] * 50000;

    $arr = array( 
        'EmpSalary' => 'Rp. '.str_replace(',','.',number_format($gaji)).',00-',
        'Absen' => $bonus = ($res['MASUK'] != 0) ? $res['MASUK'].' Hari' : 'Tidak ada Pegawai yang dipilih',
        'Bonus' => $bonus = ($gaji >= 1250000) ? 'Rp. '.str_replace(',','.',number_format(100000)).',00-' : 'Tidak ada Bonus',
        'BaSalary' => $gaji,
        'RoleName' => $res['NM_KEL'],
        'RoleID' => $res['KEL_ID']
    );

     echo json_encode($arr);
}

?>

我怎么能这样做? 如果有错误或不太明显,请指导......

0 个答案:

没有答案