AJAX不与Codeigniter Controller交配

Bra*_*rad 2 ajax jquery codeigniter

我学习AJAX和从无到有借了脚本NETTUTS笨:AJAX脚本本身完美的作品为适应我的形式,但它没有更新每个控制器的数据库.Firebug说这篇帖子在AJAX脚本中运行良好.我想我的问题是,我如何以及在哪里无法启动控制器?

form_open脚本

echo form_open('contacts/entry', $form);
Run Code Online (Sandbox Code Playgroud)

Ajax脚本(上面有验证脚本我正在展示的工作正常)

 var form_data = {
    fname: $('#fname').val(),
    lname: $('#lname').val(),
    email: $('#email').val(),
    phone: $('#phone').val(),
    relate: $('#relate').val(),
    ajax: '1'
};

$.ajax({
    url: "<?php echo site_url('contacts/entry'); ?>",
    type: 'POST',
    data: 'form_data',
    success: function() {
        $('#status').text('Update successful!'); 
    }
});

return false;   
Run Code Online (Sandbox Code Playgroud)

});

请注意,我在form_data
控制器中包含了"AJAX:1"

function entry()
{
   if ($this->input->post('ajax')) {  
   $data = array
           (
           'fname' => $this->input->post('fname'),
           'lname' => $this->input->post('lname'),
           'email' => $this->input->post('email'),
           'phone' => $this->input->post('phone'),
           'relate' => $this->input->post('relate'),
           );

   //removed validation set rules to shorten the question

   if ($this->form_validation->run() == TRUE)
   {
    $this->db->insert('contacts', $data);   
    $this->index();    
   } else
   {
    $this->index();
   }       
} 
}
Run Code Online (Sandbox Code Playgroud)

使用"if($ this-> input-> post('ajax'))"是启动控制器的正确方法吗?如果没有jquery,表单本身可以正常运行并运行控制器.所以我知道这两个单独的部分是有效的,它们只是没有啮合.谢谢

ipa*_*aus 5

首先,我将删除var form_data { ajax: '1' }并使用以下内容:

if($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest')
{
    // This is AJAX
}
else {
    // This is standard HTTP
}
Run Code Online (Sandbox Code Playgroud)

处理这个问题的好方法是使用MY_Controller.如果您使用它,添加如下函数:

protected function is_ajax() {
    return ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') ? TRUE : FALSE;
}
Run Code Online (Sandbox Code Playgroud)

你可以使用:

if($this->is_ajax())
{
    // This is AJAX
}
else {
    // This is HTTP
}
Run Code Online (Sandbox Code Playgroud)

然后,您将回显结果以将其传递给实际视图并处理操作.像这样:

if ($this->form_validation->run() == TRUE)
{
    // Tip: you've to do the data management on MODELS
    $action = $this->db->insert('contacts', $data);

    // With this method you can handle AJAX and HTTP on the same
    // validation. If is AJAX you print, if not you redirect.
    if($this->is_ajax())
        echo $action;
    else {
        // if worked, redirect the user to the success 
        // else send it to the form error
        if($action)
            redirect("form_success");
        else
            redirect("form_error");
    }
else {
    if($this->is_ajax())
        echo false;
    else {
        $this->load->view("error_form");
    }
}
Run Code Online (Sandbox Code Playgroud)

你要纠正你的jQuery代码来处理操作:

$.ajax({
    url: "<?php echo site_url('contacts/entry'); ?>",
    type: 'POST',
    data: form_data, // Like said jondavidjohn
    success: function(data) {
        if(data)
        {
            $('#status').text('Update successful!');
        } else {
            $('#status').text('Operation failed!');
        } 
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 升级到CI 2.0时,可以使用$ this-> input-> is_ajax_request(); (2认同)