ajax使用codeigniter更新数据库中的数据

时间:2022-10-06 15:59:52
 <button id="survey_act" method="post" class="tiny ui blue button" type="button" value="<?php echo $surv['id']; ?>"  >Activate Survey</button>

This is my button on click -

这是我点击的按钮 -

<script>
$(document).ready(function(){
       $(document).on("click","#survey_act", function(){ 
           alert(this.value);
                idx = this.value;

    $.ajax({
       type: "POST",
       url: "<?php echo base_url('index.php/admin/survey/act_surveyby_id/')?>/"+idx,

      }).done(function(msg){
            if(msg=="success"){
                      alert('You Successfully Activated the Survey!');
             }

                   });                       
             });
     });

    </script>

This is my javascript -

这是我的javascript -

   public function act_surveyby_id($id){
    $this->load->model('survey_m');
    if($this->survey_m->insert_activate($id)){                 
                echo "success";

    }else{

        echo "invalid";
    }   
}

This is my controller -

这是我的控制器 -

         public function insert_activate($id){

                $date = date('m-d-Y',now());
                $stat = 'Active';

                        $data = array(
                                   'issued_date' =>  $date ,
                                   'status' => $stat            
        );

                     $this->db->update('survey', $data)->where('survey_id', $id);
    if($this->db->affected_rows()>0){
            return true;
        }else{ 
                    return false;

                }

    }


  }

This is my model -

这是我的模特 -

Problem: when i click the activate survey it wont change/update the details of the survey. I really badly need a help regarding with this. Thanks . . .

问题:当我点击激活调查时,它不会更改/更新调查的详细信息。我真的非常需要帮助。谢谢 。 。 。

1 个解决方案

#1


change $.ajax function like below

如下所示更改$ .ajax功能

$.ajax({
       url: '<?php  echo base_url(); ?>index.php/admin/survey/act_surveyby_id',
       type: "POST",
       data: {
             idx : idx;
             },

and controller like below

和控制器如下

public function act_surveyby_id(){
    $id=$_POST['idx'];
    $this->load->model('survey_m');
    if($this->survey_m->insert_activate($id))
    {                 
      echo "success";

    }else{

        echo "invalid";
    }   
}

#1


change $.ajax function like below

如下所示更改$ .ajax功能

$.ajax({
       url: '<?php  echo base_url(); ?>index.php/admin/survey/act_surveyby_id',
       type: "POST",
       data: {
             idx : idx;
             },

and controller like below

和控制器如下

public function act_surveyby_id(){
    $id=$_POST['idx'];
    $this->load->model('survey_m');
    if($this->survey_m->insert_activate($id))
    {                 
      echo "success";

    }else{

        echo "invalid";
    }   
}