无法使用ajax向数据库插入数据

4

我是一名新手,对于jquery和ajax不太熟悉。现在我在使用ajax和codeigniter向数据库插入数据时遇到了问题,很难找到解决方法。当表单没有任何错误时,却出现了数据库错误,并且所有输入内容都变成了NULL。

控制器

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

1
->post['first_name'] 更改为 ->post('first_name') - Saty
@Saty,天哪!我忘记改那个了。现在它工作得很好,非常感谢。 - claudios
2个回答

4
我认为你在这里有一个语法错误。
$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')
  );

可能应该是


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

你的参数数组似乎是一个键,但它是哪个变量的呢?因此,你需要使用$data而不是'data'


1
同样的答案来自评论。变量数据只是一个打字错误,对此我很抱歉。 - claudios

3
为了在CodeIgniter中获取POST数据,我们使用:
$this->input->post('field_name');

所以您需要将所有的post['field_name']更改为post('field_name')

您的最终代码应该是:

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

请阅读https://www.codeigniter.com/user_guide/libraries/input.html了解更多有关IT技术的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接