dongyuan1970 2016-01-01 11:38
浏览 62
已采纳

无法使用带有codeigniter的ajax post jquery请求在数据库中插入数据

I want to insert row in database using ajax jquery post method for that i am using the below code in Codeigniter, but my data is not inserted in a database. Please help to sort out my issue.

View:

$("#Submit_Course_Goal").on("click", function (e) {
e.preventDefault();
var dataString = $("form#courseGoalForm").serializeArray();
alert("datastring"+dataString);
$.ajax({
    type: "post",
        url: "<?php echo base_url();?>create_course/create_course_goal",
    cache: false,               
    data: dataString,
    success: function(data){
    alert("data"+data);
    },
    error: function(){                      
    alert('Error while request..');
    }
 });
});

<form name="courseGoalForm" id="courseGoalForm" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="c_id" value="<?=$result;?>" />
<textarea data-focus="false" rows="8" name="description1"> </textarea>
<textarea data-focus="false" rows="8" name="description2">   </textarea>
<textarea data-lang="en" rows="8" name="description3">  </textarea>
<input type="submit" name="submit" value="Save" class="btn btn-primary btn btn-success" id="Submit_Course_Goal" />
</form>

Model:

public function create_course_goal($data,$id) {

   $this->load->database();
   $this->db->where('id', $id);
   $this->db->update('course', $data);
   $course_id=$id;
   if ($this->db->affected_rows() > 0) {
    return $course_id;
   }
   else
   {
   return false;
   }
}

Controller:

public function create_course_goal(){

    $course_goal1=$this->input->post('description1');
    $course_goal2=$this->input->post('description2');
    $course_goal3=$this->input->post('description3');
    $id=$this->input->post('c_id');  

    $data=array('course_goal1'=>$course_goal1,'course_goal2'=>$course_goal2,'course_goal3'=>$course_goal3);
    $result_course = $this->course_model->create_course_goal($data,$id);

    if($result_course!='false')
    {
        return true;
    }
    else
    {
        return false;
    }

}
  • 写回答

4条回答 默认 最新

  • doujiao9426 2016-01-01 12:48
    关注

    Try these codes.

    ("#Submit_Course_Goal").on("click", function (e) {
    e.preventDefault();
    var description1  = $("#description1").val();
    var description2  = $("#description2").val();
    var description3 = $("#description3").val(); 
    
    
    $.ajax({
        type: "post",
            url: "<?php echo base_url();?>create_course/create_course_goal",
        cache: false,               
        data: {
            desc1 : description1,
            desc2 : description2,
            desc3 : description3
        },
        success: function(data){
        console.log(data);
        },
        error: function(){                      
        alert('Error while request..');
        }
     });
    });
    
    <!-- Form -->
    
    <form name="courseGoalForm" id="courseGoalForm" action="" method="post" enctype="multipart/form-data" onclick="return false">
        <input type="hidden" name="c_id" value="<?=$result;?>" />
        <textarea data-focus="false" rows="8" name="description1" id="description1"> </textarea>
        <textarea data-focus="false" rows="8" name="description2" id="description2">   </textarea>
        <textarea data-lang="en" rows="8" name="description3" id="description3">  </textarea>
        <input type="submit" name="submit" value="Save" class="btn btn-primary btn btn-success" id="Submit_Course_Goal" />
    </form>
    
    
    <!-- Controller -->
    
    <?php 
    
        public function create_course_goal(){
    
    
    
        $data=array(
            'ID' => $this->input->post('c_id'),
            'course_goal1'=> $this->input->post('desc1'),
            'course_goal2'=> $this->input->post('desc2'),
            'course_goal3'=> $this->input->post('desc3')
            );
        $result = $this->course_model->create_course_goal($data);
    
        if ($result) {
          echo 'success';
        }else echo 'fail';
    
    }
    
    
    /*MODEL*/
    
    function create_course_goal($data = array())
      {
        return $this->db->insert('course',$data);
    
      }
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低