0

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

}
13
  • Also post the value of alert("datastring"+dataString); Commented Jan 1, 2016 at 11:40
  • Is there any error code / error message? Commented Jan 1, 2016 at 11:42
  • Value for datastring is [object Object],[object Object],[object Object],[object Object] Commented Jan 1, 2016 at 11:43
  • No there is no error Commented Jan 1, 2016 at 11:43
  • Saty- but in success section data is blank Commented Jan 1, 2016 at 11:45

4 Answers 4

1

have you tried this !

var dataString = $("#courseGoalForm").serialize();

instead of

var dataString = $("form#courseGoalForm").serializeArray();

Sign up to request clarification or add additional context in comments.

1 Comment

Yes used but not found the solution
0

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

  }

 ?>

Comments

0

Try this.

Controller.

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

}

Model

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

Note : course_goal1, course_goal2, course_goal3 should be same as in database. and course should be database table's name.

This was for update database if you want to insert new data use this model

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

  }

Note 2 : in your database 'id' should be primary and auto incrementing your table name should be 'course' and row names should be 'course_goal1', 'course_goal2', 'course_goal3'

7 Comments

public function testCourse(){ $data=array( 'ID' => '1', 'course_goal1'=> 'test1', 'course_goal2'=> 'test2', 'course_goal3'=> 'test3' ); $result = $this->course_model->create_course_goal($data); if ($result) { echo 'success'; }else echo 'fail'; } what is the result
btw are you triying to set a new record to database or update.
If i fill the field value manually i get the record inserted in the database.
like $data=array('course_goal1'=>'record1','course_goal2'=>'record2','course_goal3'=>'record3');
Enes- I found the problem actually its textarea i am getting value from textbox but cannot get value from textarea how to solve this
|
0

Have you tried removing method='post' from the Form and submit your data with ajax

5 Comments

I have to submit it using post method
YOu are already doing it by ajax why are you adding it two times?
I am getting value from textbox but cannot get value using textarea
have you used network tab in web console to see whether the value is submitting via post or not?
This <input type="hidden" name="c_id" value="<?=$result;?>" /> Should be like this <input type="hidden" name="c_id" value="<?php echo $result;?>" />

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.