need help to create autofill in code igniter

830 Views Asked by At

can anyone to help how create autofill form field in codeigniter 3 with AJAX, i have already browsing but no one success in progress

controller:

function autofill(){
        $id_pemesan = $this->input->post('id_pemesan', TRUE);
        $this->mod_marketing->autofill_by_id($id_pemesan,'pemesan');



            $data = array(
            'nama_depan' => nama_depan,
            'nama_belakang' => nama_belakang,
            'alamat' => alamat,
            'provinsi_kota' => provinsi_kota,
            'kode_pos' => kode_pos,
            'email' => email,
            'no_telp' => no_telp
            );

        echo json_encode($data);
    }

model :

function autofill_by_id($id_pemesan,$table){

        return $this->db->get_where($table,array('id_pemesan', $id_pemesan));
    }

script:

<script>
$(function() {
        $("#id_pemesan").change(function(){
            var id_pemesan = $("#id_pemesan").val();

            $.ajax({
                url: '<?php echo site_url('marketing/autofill'); ?>',
                type: 'POST',
                dataType: 'json',
                data: {
                    'id_pemesan': id_pemesan
                },
                success: function (pemesan) {
                    $("#nama_depan").val(pemesan.nama_depan);
                    $("#nama_belakang").val(pemesan.nama_belakang);
                    $("#alamat").val(pemesan.alamat);
                    $("#provinsi_kota").val(pemesan.provinsi_kota);
                    $("#kode_pos").val(pemesan.kode_pos);
                    $("#email").val(pemesan.email);
                    $("#no_telp").val(pemesan.no_telp);

                }
            });
        });
    });

that my latest try and its still fail

1

There are 1 best solutions below

2
On

Change the controller so it checks if response should include data, or error. You actually were not passing the row to the array, so I'm guessing this is what you wanted:

function autofill()
{
    $id_pemesan = $this->input->post('id_pemesan', TRUE);
    $row = $this->mod_marketing->autofill_by_id($id_pemesan,'pemesan');

    if( $row )
    {
        $data = array(
            'nama_depan'    => $row->nama_depan,
            'nama_belakang' => $row->nama_belakang,
            'alamat'        => $row->alamat,
            'provinsi_kota' => $row->provinsi_kota,
            'kode_pos'      => $row->kode_pos,
            'email'         => $row->email,
            'no_telp'       => $row->no_telp
        );

        echo json_encode($data);
    }

    // No rows, or there was more than one row
    else
    {
        echo json_encode(array('error' => 'No rows, or there was more than one row'));
    }
}

Change the model to check for row:

function autofill_by_id( $id_pemesan, $table )
{
    $query = $this->db->get_where($table,array('id_pemesan', $id_pemesan));

    return $query->num_rows() == 1
        ? $query->row()
        : NULL;
}

Modify the JS so you can check for errors:

$(function() {
    $("#id_pemesan").change(function(){
        var id_pemesan = $("#id_pemesan").val();

        $.ajax({
            url: '<?php echo site_url('marketing/autofill'); ?>',
            type: 'POST',
            dataType: 'json',
            data: {
                'id_pemesan': id_pemesan
            },
            success: function (pemesan) {
                if( permesan.error ){
                    alert( permesan.error );
                }else{
                    $("#nama_depan").val(pemesan.nama_depan);
                    $("#nama_belakang").val(pemesan.nama_belakang);
                    $("#alamat").val(pemesan.alamat);
                    $("#provinsi_kota").val(pemesan.provinsi_kota);
                    $("#kode_pos").val(pemesan.kode_pos);
                    $("#email").val(pemesan.email);
                    $("#no_telp").val(pemesan.no_telp);
                }
            },
            error: function(){
                alert('error 2');
            }
        });
    });
});