3

I want to add update and delete data in the table below. Delete is working. But I have a problem adding data using CRUD enter image description here

Here is the controller. I add data into two join tables, parent and student using a single form on one button click.

    public function register_students()
{

    // $this->load->model('Register_model','multi_model',TRUE);
    $encrypted_password1 = $this->encrypt->encode($this->input->post('p_pwd'));

    $parent_data = array(

        'parent_code' => $this->input->post('parent_code'),
        'f_name' => $this->input->post('p_first_name'),
        'l_name' => $this->input->post('p_last_name'),
        'DOB' => $this->input->post('p_dob'),
        'address' => $this->input->post('p_address'),
        'tel' => $this->input->post('p_tel_no'),
        'email' => $this->input->post('email'),
        'username' => $this->input->post('p_username'),
        'password' => $encrypted_password1,
    );



     $id = $this->Model_Action->insertTable('parent',$parent_data);




    $encrypted_password = $this->encrypt->encode($this->input->post('pwd'));
    $student_data = array(
        's_id' => '',
        'student_code' => $this->input->post('student_code'),
        'f_name' => $this->input->post('first_name'),
        'l_name' => $this->input->post('last_name'),
        'DOB' => $this->input->post('dob'),
        'gender' => $this->input->post('gender'),
        'address' => $this->input->post('address'),
        'tel' => $this->input->post('tel_no'),
        'username' => $this->input->post('username'),
        'password' => $encrypted_password,
        'p_id' => $id


    );
    $insert = $this->Model_Action->insertTable('student',$student_data);

    echo json_encode(array("status" => TRUE));

    redirect('student');


}

Here is my model

 function insertTable($table, $data) {
    $this->db->insert($table, $data);
    return $this->db->insert_id();
}

This is my view - javascript section. I have removed edit function and delete function in this section.

<script type="text/javascript">


 $(document).ready( function () {
      $('#student_table').DataTable();
  } );
    var save_method; 
    var table;



function add_book()
{
  save_method = 'add';
  $('#form')[0].reset(); 
  $('#modal_form').modal('show'); 
}


function save()
{
  var url;
  if(save_method == 'add')
  {
    url = "<?php echo site_url('/Student/register_students')?>";
  }
  else
  {
    url = "<?php echo site_url('/Student/student_update')?>";
  }

   // ajax adding data to database
      $.ajax({
        url : url,
        type: "POST",
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data)
        {
           //if success close modal and reload ajax table
           $('#modal_form').modal('hide');
          location.reload();// for reload a page
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error adding / update data');
        }
    });
}

I have used bootstrap model for adding and updating forms and those are also working fine.

2
  • 1
    did you check if you get any $data? Did you check, if the database columns match the $data array keys? AND what does your browser console say? Commented Feb 24, 2019 at 17:26
  • yeah, because I tried adding data without ajax, (using form action method) and It did work. Commented Feb 24, 2019 at 17:55

1 Answer 1

1

As I see, you are using redirect('student'); on ajax call. Dot do it if you want work with ajax. I advise you:

$array = array(
    'link' => 'strudent',
    'status' => true
);

echo json_encode($array);
return;

And on ajax success:

window.location.href = data.link;
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, Now I can update However I have to reload the page. and when I click the 'save' button it shows that error 'error adding/ updating data' But the data get updated.
Your answer is very helpful, I hope you would help me solve this as well. :)
Maybe you have a problem with url which you call ajax??
Thank you I found the error, I have only taken one table data for ajax table view. So I wrote a join query. now it's all working. Thank you for your help :)
Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.