0

Good morning everyone from Indonesia.

I have some web project with Codeigniter framework. I have some fieldset for insert some value input tag with radio button. I'm using some Wizard Function for every single fieldset. In the end of each Wizard, We have some button. We add some javaScript function on this button. In javaScript function, we add some Ajax function. Ajax function can sending some value from "view" to "controller". After that, on "controller" we save the data with "model" to database.

The question is, why I can't save the data to database with ajax function I have made before?

This is "view" (javascript and ajax)

function prosesFieldset1(){
        var radios = document.getElementsByName('f3');
        for (var r=0, length = radios.length;  r < length; r++){
            if(radios[r].checked){
                var dataf3 = radios[r].value;
                if (dataf3 == 1 || dataf3 == 2 ){
                    _('fieldset1').style.display = 'none';
                    _('fieldset2').style.display = 'block';
                    _('progressBar').value = 25;
                }else{
                    _('fieldset1').style.display = 'none';
                    _('fieldset3').style.display = 'block';
                    _('progressBar').value = 25;
                }
            }
        }
        var f3 = dataf3;
        $.ajax({
            url     : "<?php echo site_url('TracerStudy/masuk_data_ts');?>",
            type    : 'post',
            data    : {'f3' : f3, 'proses' : 1},
            success : function(data){
                console.log(data);
            },
        });
    }

This is the "controller" for get data from Ajax on "view" to restore them in to new variable and save them to database (TracerStudy/masuk_data_ts)

public function masuk_data_ts(){
$f3 = $this->input->post('f3');
$proses = $this->input->post('proses');

if($proses == 1){
  $id_alumni = $this->session->userdata('id_alumni');
  $udah = $this->TracerStudy_model->get_data_ts($id_alumni);
  if($udah = 1 ){
    $data = array(
      'id_alumni_fk' => $this->session->userdata('id_alumni'),
      'f3' => $f3
    );
    // $proses = 0;
    $this->TracerStudy_model->update_data_ts($data);
  }
  else{
    $data = array(
      'id_alumni_fk' => $this->session->userdata('id_alumni'),
      'f3' => $f3,
      'status' => $proses
    );
    // $proses = 0;
    $this->TracerStudy_model->simpan_data_ts($data);
  }
}

This is "model" to save data into database (TracerStudy_model/simpan_data_ts)

  public function simpan_data_ts($data){
  $this->db->insert("tracer_study", $data);
}

I have appreciate for any suggestion, and solution to this problem. Thank you everyone, I hope for all of you, have a nice day today.

hambaBiasa
  • 19
  • 6
  • As practice, I recommend you to check if the variables have desired value first. It helps to detect where the problem starts from. – Ukasha May 23 '17 at 00:27
  • I don't know php but something tells me that url is still invalid. – Darkrum May 23 '17 at 00:30
  • Thank you @Ukasyah , from your suggestion, in which part I've must check the variable that make this problem ? – hambaBiasa May 23 '17 at 00:35
  • Thank you @Darkrum , I think so, maybe url on ajax can cause this problem. – hambaBiasa May 23 '17 at 00:36
  • If it caused by url, maybe this can help. https://stackoverflow.com/questions/21246818/how-to-get-the-base-url-in-javascript – Ukasha May 23 '17 at 01:20

1 Answers1

0
 $.ajax({
        url     : "<?php echo site_url('TracerStudy/masuk_data_ts');?>",
        type    : 'post',
        data    : {f3 : f3, proses : 1},
        success : function(data){
            console.log(data);
        },
    });

don't use 'f3' , 'proses' like wise in data.

chamina
  • 498
  • 3
  • 15