4

I am new to jquery and ajax and now I have a hard time finding a fix to this problem of mine when inserting data into database using ajax and codeigniter. All errors are okay but when there's no error on the form, I get a database error and all the inputs become NULL.

Controller

public function add () {

 $this->load->model('user_model');
    $data => array (
      'first_name'      => $this->input->post['first_name'],
      'last_name'       => $this->input->post['last_name'],
      'active'          => $this->input->post['active'],
      'date_registered' => date('Y/m/d h:i:sa')
  );

  // assume validation rules are already set.
  if ($this->form_validation->run() == FALSE) {
   $result['message'] = validation_errors();
  } else {
   $result['data'] = $this->user_model->save($data);
  } 
 }

Ajax 1:

$(document).ready(function() { 
  $('#create-user').click( function(e) {
    var is_valid  = false;
    var form_id   = '#'+ $(this).parents('form').attr('id');
    // Validate required fields are not blank
    // do a js validation?

    // Apply action
    if(is_valid) {
      var add_result = do_submit(form_id);
    } else {
      $('#error-msg').html(result.message); // if form is not valid
    }
  });
});

Ajax 2:

function do_submit(form_id) {
  var url         = $(form_id).attr("action");
  var ajax_result = false;
  var formData    = {};

  // Submit form using ajax
  ajax_result = $.ajax({
    type: "POST",
    url: url,
    data: $(form_id).serialize(),
    dataType: 'json',
    success: function(result) {
      // return result; 
      //  do something
      console.log(result);
      if (result.data) {
        make_alert();
      }
    },
    error: function(textStatus) {
      /* Note: decide how all errors should be shown. */
      swal({
        title: "Error!",
        text: "Oops, something went wrong. Check fields and try again.",
        type: "error"
      });
    }
  });

  return ajax_result;
} // End do_submit()
2
  • 1
    change ->post['first_name'] to ->post('first_name') Commented May 13, 2016 at 6:11
  • @Saty, Holy Cow! I forgot to change that. Now it's working fine, Thanks so much. Commented May 13, 2016 at 6:18

2 Answers 2

4

I think you have a syntax error here

$this->load->model('user_model');
'data' => array (
  'first_name'      => $this->input->post['first_name'],
  'last_name'       => $this->input->post['last_name'],
  'active'          => $this->input->post['active'],
  'date_registered' => date('Y/m/d h:i:sa')
  );

Should probably be

$this->load->model('user_model');
$data => array (
  'first_name'      => $this->input->post('first_name'),
  'last_name'       => $this->input->post('last_name'),
  'active'          => $this->input->post('active'),
  'date_registered' => date('Y/m/d h:i:sa')
);

Your parameter array seems to be a key, but of what variable? So you need to have $data instead of 'data'.

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

1 Comment

same answer from the comment. and the variable data is only a typo error sorry for that.
3

To get post data in codeigniter we use

$this->input->post('field_name');

SO you need to change all post['field_name'] to post('field_name')

Your final code would be

 $this->load->model('user_model');
        $data => array (
          'first_name'      => $this->input->post('first_name'),
          'last_name'       => $this->input->post('last_name'),
          'active'          => $this->input->post('active'),
          'date_registered' => date('Y/m/d h:i:sa')
      );

Read https://www.codeigniter.com/user_guide/libraries/input.html

Comments

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.