2016-05-13 123 views
4

我是jquery和ajax的新手,現在我很難找到解決這個問題的方法,當使用ajax和codeigniter將數據插入到數據庫時。 所有的錯誤都可以,但是當表單上沒有錯誤時,我得到一個數據庫錯誤,所有的輸入都變爲NULL。不能使用ajax插入數據到數據庫

控制器

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

變化' - >將['first_name']'發佈到' - > pos t('first_name')' – Saty

+0

@Saty,聖牛!我忘了改變這一點。現在它工作正常,非常感謝。 – claudios

+0

好的再次感謝 – claudios

回答

3

要獲得職位數據笨我們使用

$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

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

我認爲你的建議$數據也是正確的! – Saty

+1

與評論相同的答案。而可變數據只是一個錯誤的錯誤而已。 – claudios