1

I have a script like this:

$(document).on("click", ".passingID", function () {
         var ids = $(this).attr('data-id');
         $("#idkl").val( ids );
         $.ajax({
              method: "POST",
              url: "getidforeditkaryawan.php",
              data: { idkl: ids}
        }).done(function(data) {
            console.log(data);
            $("#nik").val(data['nik']);
            $("#ktp").val(data['ktp']);
            $("#status").val(data['status']);
            $("#nama").val(data['nama']);
            $("#bank").val(data['bank']);
            $("#norek").val(data['norek']);
            $("#tglmsk").val(data['tgl_msk']);
            $("#tglkl").val(data['tgl_keluar']);
            $("#idp").val(data['id_perusahaan']);
            $("#tlk").val(data['tlk_individu']);
          }).fail(function( jqXHR, textStatus ) {
              console.log(jqXHR, textStatus);
        });
    });

and this is getidforeditkaryawan.php file:

<?php
   $con = mysqli_connect("127.0.0.1","root","","penggajian"); 
   $sql = "SELECT * FROM karyawan_lapangan WHERE id_kl = '".$_POST['idkl']."'";
   $getdatakaryawan = mysqli_query($con,$sql);
   $data = mysqli_fetch_assoc($getdatakaryawan);
   print_r(json_encode($data));
?>

but everytime i try to print the value inside the 'data' variable like this

console.log(data['nik']);

the value won't appear

the result from print_r(json_encode($data)); is this:

{"id_kl":"4","nik":"13","ktp":"1231231","status":"aktif","nama":"ardia","bank":"asdasd","norek":"21313","tgl_masuk":"2019-02-14","tgl_keluar":"0000-00-00","id_perusahaan":"3123121","tlk_individu":"4124124"}
6
  • Use print(json_encode($data)); instead of print_r Commented Feb 5, 2019 at 15:37
  • Where is your php file located? Is it in the same folder as your js? Commented Feb 5, 2019 at 15:37
  • @pr1nc3 yes it is on the same folder Commented Feb 5, 2019 at 15:39
  • And are you sure that your query returns you the data and there are no php errors there? your console.log you have under done function returns the data you need? Commented Feb 5, 2019 at 15:39
  • yes, it returns the data i need, look at my question again i put the result from print_r(json_encode($data)); Commented Feb 5, 2019 at 15:44

1 Answer 1

2

Since you return a json object you can access it like:

$("#nik").val(data.nik);

Edit based on 04FS comment:

 $.ajax({
              method: "POST",
              dataType: "json",
              url: "getidforeditkaryawan.php",
              data: { idkl: ids}
        }).

Missed that spot, he is right.

Sign up to request clarification or add additional context in comments.

1 Comment

@kelvinsutanto: ohh i little bit late here? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.