douzai2562 2017-03-31 12:43
浏览 35

如何使用Model Class Codeigniter上的Ajax将数据存储到数据库

We want to put data from view () to database. We use Ajax to running some background process (insert data to database). But We can't save the data to database. I'm appreciate and thank's a lot for any suggest and good avice do you give. I'm sorry if this is just a newbie's question.

This is the View (form, fieldset, javascript, and ajax)

var popup, x, f31, f32, f33, f4, f5, f6, f7, f7a, f81, f82, f11, f14, f161, f17a1;

function _(x){
    return document.getElementById(x);
}

//fungsi tutup popup
function tutupPopup(){
    _("popup").style.display = "none";
}

function prosesc11(){
    f31 = _("f31").value;
    f32 = _("f32").value;
    f33 = _("f33").value;
    $.ajax({
        url    : "<?php echo base_url().'index.php/TracerStudy_model/simpan_data_ts';?>",
        type   : 'post',
        data   : {'f31' : f31, 'f32' : f32},
        success: function(data){
            console.log(data);
        }
    });

    if(f31.length>0||f32.length>0){
        _("formc1-1").style.display = "none";
        _("formc1-2").style.display = "block";
        _("progressBar").value = 50;
    }else{
        alert("Maaf data anda ada yang belum diisi.")
    }
}
<form class="multiphaseForm" id="multiphaseForm" onsubmit="return false"  
    method="post" action="<?php base_url();?>TracerStudy/masuk_data_ts"
>
<fieldset class="formc1-1" id="formc1-1"><!--fieldset pertama-->
    <!-- f3 -->
    <div class="form-group">
        <div class="row">
            <div class="col-md-8">
                <label>1. Kapan anda mulai mencari pekerjaan ?</label>
            </div>
        </div>
        <div class="row ">
            <input class="col-lg-1" id="f31" name="f3" type="radio" value="1" class="form-control">
            <label class="col-md-8" for="f31">Sebelum lulus</label>
        </div>
        <div class="row ">
            <input class="col-lg-1" id="f32" name="f3" type="radio" value="2" class="form-control">
            <label class="col-md-8" for="f32">Setelah lulus</label>
        </div>
        <div class="row ">
            <input class="col-lg-1" id="f33" name="f3" type="radio" value="3" class="form-control">
            <label class="col-md-8" for="f33">Tidak mencari kerja</label>
        </div>
    </div>
    <!-- <input class="btn_c1-1" id="simpan" name="simpan" type="submit" onclick="prosesc11" value="simpan"/> -->
    <button onclick="prosesc11()">Selanjutnya</button>
</fieldset><!--formc1-1-->
</form>

This is Model Class

public function simpan_data_ts(){
      $idAlumni = $this->session->userdata('id_alumni');
      // $default = null;
      // $satu = $this->input->post('f3');
      // $datetime = date('Y-m-d');
      // $dataSimpan = array( 'f3' => $satu);

      //$f31 = $this->input->post('f31');
      $f32 = $this->input->post('f32');
      $data = array(
        'id_alumni_fk' => $idAlumni,
        'f3' => $this->input->post('f31')
      );
      $this->db->insert('tracer_study', $data);
}

We hope, anyone can tell us what's wrong with it. And I have to say thank you for all your response.

</div>
  • 写回答

2条回答 默认 最新

  • drq22639 2017-03-31 12:50
    关注

    You can't call the model class directly from ajax, as a model class can't be accessed from an HTTP request. You have to pass it first to a controller function, for example with a controller called Foo with a function called bar() the ajax you have would be:

    $.ajax({
                url    : "<?php echo base_url().'index.php/foo/bar';?>",
                type   : 'post',
                data   : {'f31' : f31, 'f32' : f32},
                success: function(data){
                    console.log(data);
                }
            });
    

    Then in the Foo::Bar function, load model and call your function to save the data:

    public function bar() {
    $this->load->model('your_model_name');
    $this->your_model_name->simpan_data_ts($this->input->post);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题