0

I want my form checks my input directly using ajax and jQuery, but it cant work

this is my form input

<div class="form-group">
    <label for="exampleInputEmail1">NIP</label>
    <input type="text" name="nip" id="nip" class="form-control" placeholder="NIP" maxlength="18" required>
    <span id="pesannip" name="pesannip"></span>
</div>

jQuery from the input form above

<script>
  $(document).ready(function(){
    $('#nip').blur(function(){
      $('#pesannip').html('sedang mengecek');
      var nip = $(this).val();
      var len=nip.length;
      if (len==0) {
        $('#pesannip').text("NIP tidak boleh kosong");
        $('#nip').css('border', '3px #C33 solid');
      }
      else {
        if (!valid_notelp(nip)) {
          $('#pesannip').text("NIP tidak valid");
          $('#nip').css('border', '3px #C33 solid');
        }
        else if (len>0 && len<18) {
          $('#pesannip').text("NIP terlalu pendek, harus 18 angka");
          $('#nip').css('border', '3px #C33 solid');
        }else {
          $.ajax({
            type : 'POST',
            url : "<?php echo base_url('index.php/dosen/cek_nip_dosen') ?>",
            data : 'nip='+nip,
            success : function(data)
            {
              if (data=='gada') 
              {
                $("#pesannip").html("NIP bisa digunakan");
                $('#nip').css('border', '3px #090 solid');
              }
              else if(data=='ada')
              {
                $("#pesannip").html("NIP sudah digunakan oleh pengguna lain");
                $('#nip').css('border', '3px #C33 solid');
              }
            }
          });
        }
      }
    });

    function valid_notelp(notelp){
    var pola = new RegExp(/^[0-9-+]+$/);
    return pola.test(notelp);
    }
  });
</script>

my model named model_dosen

public function cek_nip($nip)
    {
        $cek = $this->db->query("select * from detail_dosen where nip='$nip'");
        return $cek->result();
    }

and last my controller named dosen

public function cek_nip_dosen()
    {
        $this->load->model("model_dosen");
        $nip = array('nip' => $this->input->post('nip'));
        $hasil_cek = $this->model_dosen->cek_nip('$nip');
        if (count($hasil_cek)==0)
        {
            echo 'gada'; //data not exist
        }
        else 
        {
            echo 'ada';
        }
    }

when i insert any data (type of data number) the span always show "NIP bisa digunakan" that mean data i input not exist in the database even the data is exist in the database.

can someone help me.

thanks in advance.

3 Answers 3

3

Don't wrap your variable name in single quotes in cek_nip_dosen() method. You need to change the following line:

$hasil_cek = $this->model_dosen->cek_nip('$nip');

to

$hasil_cek = $this->model_dosen->cek_nip($nip);
Sign up to request clarification or add additional context in comments.

3 Comments

that doesnt work, even the ajax looks not work with that way
@NoerielHilal try to var_dump the $hasil_cek variable in your controller. Make sure that you've the matching record in database.
i've solve my problem, $nip = array('nip' => $this->input->post('nip')); is wrong way , it must be $nip = $this->input->post('nip'); . thanks for your helping
0

Try with

$hasil_cek = $this->model_dosen->cek_nip($nip);

1 Comment

that doesnt work, even ajax does not seem to work that way
0

Change your ajax data option:

from:

data : 'nip='+nip

to:

data: {"nip":nip}

and change your model input from '$nip' to $nip

3 Comments

can you send ajax header from console?
must i add ajax header from console? i have used ajax, and it works well, i just start using ajax on codeigniter
Please sure that nip data was posted to your controller and change you model input from '$nip' to $nip

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.