【问题标题】:Can't insert data into database using ajax无法使用ajax将数据插入数据库
【发布时间】:2016-05-13 06:10:39
【问题描述】:

我是 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);
  } 
 }

阿贾克斯 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
    }
  });
});

阿贾克斯 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()

【问题讨论】:

  • ->post['first_name']更改为->post('first_name')
  • @Saty,圣牛!我忘了改变那个。现在它工作正常,非常感谢。

标签: php jquery ajax codeigniter


【解决方案1】:

我认为你这里有语法错误

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

【讨论】:

  • 评论中的相同答案。并且变量数据只是一个错字错误对不起。
【解决方案2】:

要在我们使用的 codeigniter 中获取发布数据

$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

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2016-10-12
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多