php无法获取ajax的数据库,php – 无法使用ajax将数据插入数据库

我是

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值