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

时间:2016-01-01 11:38:13

标签: php jquery ajax codeigniter

我想使用ajax jquery post方法在数据库中插入行,因为我在Codeigniter中使用下面的代码,但是我的数据没有插入数据库中。 请帮助解决我的问题。

  

查看:

$("#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>
  

型号:

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;
   }
}
  

控制器:

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 个答案:

答案 0 :(得分:1)

你尝试过这个吗?

var dataString = $(&#34;#courseGoalForm&#34;)。serialize();

而不是

var dataString = $(&#34; form#courseGoalForm&#34;)。serializeArray();

答案 1 :(得分:0)

试试这个。

控制器。

public function create_course_goal(){



    $data=array(
        'ID' => $this->input->post('c_id'),
        'course_goal1'=> $this->input->post('description1'),
        'course_goal2'=> $this->input->post('description2'),
        'course_goal3'=> $this->input->post('description3')
        );
    $result = $this->course_model->create_course_goal($data);

    if ($result) {
      echo 'success';
    }else echo 'fail';

}

模型

 function create_course_goal($options = array())
  {

    if(isset($options['course_goal1']))
      $this->db->set('course_goal1',$options['course_goal1']);;
    if(isset($options['course_goal2']))
      $this->db->set('course_goal2',$options['course_goal2']);;
    if(isset($options['course_goal3']))
      $this->db->set('course_goal3',$options['course_goal3']);;


    $this->db->where('ID',$options['ID']);
    $this->db->update('course');
    return $this->db->affected_rows();
  }

注意:course_goal1,course_goal2,course_goal3应与数据库中的相同。当然应该是数据库表的名称。

如果要插入新数据,请使用此模型

function addNewData($data = array())
  {
    return $this->db->insert('course',$data);

  }

注2:在您的数据库中&#39; id&#39;应该是主要和自动递增 你的桌子名称应该是“#39;当然&#39; 和行名称应为&#39; course_goal1&#39;,&#39; course_goal2&#39;,&#39; course_goal3&#39;

答案 2 :(得分:0)

您是否尝试过删除方法=&#39; post&#39;从表单中并使用ajax

提交数据

答案 3 :(得分:0)

试试这些代码。

 
("#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);

  }

 ?>