代码点火器验证-Ajax,错误:validation-> run为true时功能正常工作

时间:2018-07-27 14:02:04

标签: ajax codeigniter validation

嗨,我尝试使用ajax制作验证表单。当文本框为空时,没有问题,一切正常,并给出正确的错误。但是,当我填写表格Ajax时,其他方法就无法正常工作,并且会转到error:function。请您帮忙! 当我启用dataType:“ JSON”时, 我看到输出 假 Basvuru:1379 {isim:“

İsimZorunludur!

”,soyad:“

Soyad Zorunludur!

”,emailadresi:“

给Adresi Zorunludur发电子邮件!” p>“,ilce:”“,il:”“,...}

当我禁用dataType:“ JSON”时,ı可能会看到post数据的console.log(data),否则会出错:function

控制器:

function basvuru_ekle()
    {


    $this->form_validation->set_rules('isim', 'İsim', 'required');
    $this->form_validation->set_rules('soyad', 'Soyad', 'required' );
    $this->form_validation->set_rules('emailadresi', 'Email Adresi', 'required' );
    $this->form_validation->set_rules('ilce', 'İlçe', 'required' );
    $this->form_validation->set_rules('il', 'İl', 'required' );
    $this->form_validation->set_rules('adres', 'Adres', 'required' );
    $this->form_validation->set_rules('kordinat', 'Kordinat', 'required' );

    //$this->form_validation->set_error_delimiters('Hata:', '');
    $this->form_validation->set_message('required', ' {field} Zorunludur!');

    if ($this->form_validation->run() == FALSE) {

        $data = array(
            'isim' => form_error('isim'),
            'soyad' => form_error('soyad'),
            'emailadresi' => form_error('emailadresi'),
            'ilce' => form_error('ilce'),
            'il' => form_error('il'),
            'adres' => form_error('adres'),
            'kordinat' => form_error('kordinat'),
            'status'=> FALSE

        );

        echo json_encode($data);

    } 
    else {

            $basvurubiletnumarasi = strftime("%Y%m%d%H%M%S");
            $basvurudurumu = "1";

            $data = array(
                'basvurubiletnumarasi' => $basvurubiletnumarasi,
                'isim' => $this->input->post('isim') ,
                'soyad' => $this->input->post('soyad') ,
                'emailadresi' => $this->input->post('emailadresi') ,
                'ilce' => $this->input->post('ilce') ,
                'il' => $this->input->post('il') ,
                'ilce' => $this->input->post('ilce') ,
                'adres' => $this->input->post('adres') ,
                'kordinat' => $this->input->post('kordinat') ,
                'basvurudurumu' => $basvurudurumu,

                // 'olusturulmatarihi' => $this->input->post('olusturulmatarihi'),

            );


        $insert = $this->basvuru_model->basvuru_ekle($data);
        echo $data=json_encode(array("status" => TRUE));


        }
    }

查看Ajax:

function save()
       {
         var url;
         if(save_method == 'add')
         {
             url = "<?php echo site_url('index.php/basvuru/basvuru_ekle')?>";
         }
         else
         {
           url = "<?php echo site_url('index.php/basvuru/basvuru_guncelle')?>";
         }

          // ajax adding data to database

              $.ajax({
              type:"POST",
              url:url,
              data:$('#form').serialize(),
             dataType:"JSON",
              success:function (data) {
              //  var obj = $.parseJSON(data);
            //  $('#data1').html(data);
            $('#isim1').html(data.isim);
            $('#soyad1').html(data.soyad);
            $('#emailadresi1').html(data.emailadresi);
            $('#ilce1').html(data.ilce);
            $('#il1').html(data.il);
            $('#adres1').html(data.adres);
            $('#kordinat1').html(data.kordinat);

              console.log(data.status);
               // alert(data.sonuc);

              if(data.status){
                console.log("false");

              }
              else{
                console.log("true");
               console.log(data);
              }


              },
              error:function(data){
                console.log("error");
              }

        }); 



       }

型号:

function basvuru_ekle($data)
    {
    print_r($data);
    $this->db->insert($this->table, $data);
    return $this->db->insert_id();
    }

2 个答案:

答案 0 :(得分:0)

删除插入函数中的print_r(),然后尝试以下操作:

function save() {
    var url;
    if (save_method == 'add') {
        url = "<?php echo site_url('index.php/basvuru/basvuru_ekle')?>";
    } else {
        url = "<?php echo site_url('index.php/basvuru/basvuru_guncelle')?>";
    }

    // ajax adding data to database

    $.ajax({
        type: "POST",
        url: url,
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data) {

            if (data.status == false) {
                console.log('false');
                $('#isim1').html(data.isim);
                $('#soyad1').html(data.soyad);
                $('#emailadresi1').html(data.emailadresi);
                $('#ilce1').html(data.ilce);
                $('#il1').html(data.il);
                $('#adres1').html(data.adres);
                $('#kordinat1').html(data.kordinat);
            } else {
                console.log("true");
            }
        },
        error: function(data) {
            console.log("error");
        }

    });



}

答案 1 :(得分:0)

我检查发现了确切的问题。正如亚历克斯所说,问题是由于模式中的Print_r()。删除后,现在可以正常使用了。 解决问题后。 查看:

myclass *ptr =new myclass();
int offset_y=//whatever offset of y in number of bytes from base of object of perticular class;
char *byte_ptr_y=((char*)ptr)[offset_y];
DATATYPE y=*((DATATYPE*)byte_ptr_y);

控制器:

function save()
       {
         var url;
         if(save_method == 'add')
         {
             url = "<?php echo site_url('index.php/basvuru/basvuru_ekle')?>";
         }
         else
         {
           url = "<?php echo site_url('index.php/basvuru/basvuru_guncelle')?>";
         }

          // ajax adding data to database

              $.ajax({
              type:"POST",
              url:url,
              data:$('#form').serialize(),
             dataType:"JSON",
             success: function(data) {
               if (data.status == false) {
                 console.log('false');
                 $('#isim1').html(data.isim);
                 $('#soyad1').html(data.soyad);
                 $('#emailadresi1').html(data.emailadresi);
                 $('#ilce1').html(data.ilce);
                 $('#il1').html(data.il);
                 $('#adres1').html(data.adres);
                 $('#kordinat1').html(data.kordinat);
                 } else {
                   console.log("true");
                   $('#modal_form').modal('hide');
                   location.reload();// for reload a page
                   }
                   },
                   error: function (jqXHR, textStatus, errorThrown) { 
                     if (jqXHR.status == 404) { alert('AJAX page not found.'); 
                     } 
                     else { 
                       alert('AJAX Error: ' + textStatus + ': ' + errorThrown); } 
                       }
                       }); 



       }

型号:

function basvuru_ekle()
    {


    $this->form_validation->set_rules('isim', 'İsim', 'required');
    $this->form_validation->set_rules('soyad', 'Soyad', 'required' );
    $this->form_validation->set_rules('emailadresi', 'Email Adresi', 'required' );
    $this->form_validation->set_rules('ilce', 'İlçe', 'required' );
    $this->form_validation->set_rules('il', 'İl', 'required' );
    $this->form_validation->set_rules('adres', 'Adres', 'required' );
    $this->form_validation->set_rules('kordinat', 'Kordinat', 'required' );

    //$this->form_validation->set_error_delimiters('Hata:', '');
    $this->form_validation->set_message('required', ' {field} Zorunludur!');

    if ($this->form_validation->run() == FALSE) {

        $data = array(
            'isim' => form_error('isim'),
            'soyad' => form_error('soyad'),
            'emailadresi' => form_error('emailadresi'),
            'ilce' => form_error('ilce'),
            'il' => form_error('il'),
            'adres' => form_error('adres'),
            'kordinat' => form_error('kordinat'),
            'status'=> FALSE

        );
        echo json_encode($data);

    } 
    else {

            $basvurubiletnumarasi = strftime("%Y%m%d%H%M%S");
            $basvurudurumu = "1";

            $data = array(
                'basvurubiletnumarasi' => $basvurubiletnumarasi,
                'isim' => $this->input->post('isim') ,
                'soyad' => $this->input->post('soyad') ,
                'emailadresi' => $this->input->post('emailadresi') ,
                'ilce' => $this->input->post('ilce') ,
                'il' => $this->input->post('il') ,
                'ilce' => $this->input->post('ilce') ,
                'adres' => $this->input->post('adres') ,
                'kordinat' => $this->input->post('kordinat') ,
                'basvurudurumu' => $basvurudurumu,

                // 'olusturulmatarihi' => $this->input->post('olusturulmatarihi'),

            );



        $insert = $this->basvuru_model->basvuru_ekle($data);
        echo json_encode(array(
            "status" => TRUE,
        ));


        }
    }