php - Codeigniter 3 ajax calculating from another input using jquery

663

So the idea is to manipulate the value that is delivered by Ajax. This value should be divided by 20 and the result should be multiplied by the value from #nilai_hari input. Here's my code

// trigger
<select id="id_anggota" name="id_anggota">
  <option>-Pilih Karyawan</option>
  <?php $no=1; foreach ($listAnggota as $l) {?>
    <option value="<?php echo $l->id_anggota; ?>">
      <?php echo $l->nama_lengkap; ?>
    </option>
    <?php $no++; }?>
</select>
// the effected
<select id="tunjangan_makan" name="tunjangan_makan"></select>
// another trigger
<input type="text" id="nilai_hari" name="nilai_hari" >

And here's the script

$('#id_anggota').change(function() {
    var id = $(this).val();
    $.ajax({
        url: "<?php echo base_url();?>hrd/penggajian/get_subpenggajian",
        method: "POST",
        data: {
            id: id
        },
        async: false,
        dataType: 'json',
        success: function(data) {
            var html = '';
            var i;
            for (i = 0; i < data.length; i++) {
                if (data[i].flat_tunjangan == 1) {
                    html += '<option value="' + data[i].tunjangan_makan + '">' + number_format(data[i].tunjangan_makan, 0, '', '.') + '</option>';
                } else {
                    $('#tunjangan_makan', '#nilai_hari').keyup(function() {
                        var nilai_hari = $("#nilai_hari").val(),
                            makan = ((data[i].tunjangan_makan / 20) * nilai_hari)
                        html += '<option value="' + makan + '">' + number_format(makan, 0, '', '.') + '</option>';
                    })
                }
            }
            $('.tunjangan_makan').html(html);
        }
    });
});

So, the value of the data in which flat_tunjangan is 0 must be calculated from another input. Thanks in advance

247

Answer

Solution:

You are doing it wrong. You should call other input onkey event and call same ajax request again and remove that onkey event inside $('#id_anggota').change().

$('#id_anggota').change(function(){
        var id=$(this).val();
        
        // perform ajax and get data
        var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];
        
        var html = '';
        var i;
        for(i=0; i<data.length; i++){
          if(data[i].flat_tunjangan == 1){
            html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';
          } else {
              var nilai_hari  = $("#nilai_hari").val(), 
              makan = ((data[i].tunjangan_makan/20)*nilai_hari)
              
              if (makan != '') {
                html += '<option value="'+makan+'">'+makan+'</option>';
              }
          }
        }
        $('.tunjangan_makan').html(html);
 });
 
 $("#nilai_hari").keyup(function(){
    var nilai_hari = $(this).val();
    
    // again do ajax reqeust and fetch data using selected tunjangan_makan id
    var id = $('#id_anggota').val();
    var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];
    
    var html = '';
    var i;
    for(i=0; i<data.length; i++){
      if(data[i].flat_tunjangan == 1){
        html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';
      } else {
          var nilai_hari  = $("#nilai_hari").val(), 
          makan = ((data[i].tunjangan_makan/20)*nilai_hari)

          if (makan != '') {
            html += '<option value="'+makan+'">'+makan+'</option>';
          }
      }
    }
    $('.tunjangan_makan').html(html);
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="id_anggota" name="id_anggota">
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
</select>

<select id="tunjangan_makan" name="tunjangan_makan"></select>

<input type="text" id="nilai_hari" name="nilai_hari" >



People are also looking for solutions of the problem: 403 this action is unauthorized.
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

938 php - Codeigniter 3 ajax calculating from another input using jquery
314 php - CodeIgniter Database 'autoinit' not working
9 jquery - Php returning encoded data to ajax when it's not even in json_encode
693 How to push array elemnt to specific position in another array using php
592 php - foreach echo into 2 different input from json
896 php - Read the returned JSON from CURL in codeigniter controller
694 Using html form to import csv data... using PHP script load data infile and MySQL
625 php - Haveing one drop down selection box populate another from Mysql database
5 Issue passing an array from PHP to Javascript (Jquery) as shown in common answers
320 command line - php prevent '`rm -rf ~/`;'

People are also looking for solutions to the problem: php - Save property type int google datastore

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.