Welcome Guest, Not a member yet? Register   Sign In
AJAX AND JSON
#5

[eluser]asylmottaket[/eluser]
Here is what im using in the controller:


Code:
if($this->input->post('submit'))
{
    if ($this->form_validation->run('some_definition_from_config_file') === TRUE)
    {
        $post_array = $this->_post_array( $_POST );

        if($this->personalia_model->save_post($post_array))
        {
            if($this->_is_ajax())
            {
                // SOME AJAX RETURN STUFF
                // Set header, return som json_encoded blah blah
            }
            else
            {
                // IF NOT JAVASCRIPT, DO SOME OTHER STUFF
            
                $this->session->set_flashdata('status', 'Submission successful!');

                redirect('/admin/blog/view/' . $id);

                exit;
            }
        }
    }
    else
    {
        if($this->_is_ajax())
        {
            // OPTIONAL RETURN CI FORM VALIDATION ERRORS FROM validation_errors()
        }
    }
}


// AND THE OTHER METHODS THAT RETURN POST VALUES AND CHECKS IF AJAX IS USED

function _post_array($post_vars)
{
    $this->load->helper('security');
    
    $post_array = array();
    
    foreach ($post_vars as $key => $value)
    {    
        $post_array[$key] = xss_clean($value);
        
        if(substr($key, 0, 6) == 'submit')
        {
            unset($post_array[$key]);
        }
    }
    
    return $post_array;
}

function _is_ajax()
{
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}



I also use jQuery, and with the jQuery form plugin, ajax form submission is quite easy


Messages In This Thread
AJAX AND JSON - by El Forum - 12-07-2009, 03:20 PM
AJAX AND JSON - by El Forum - 12-07-2009, 04:57 PM
AJAX AND JSON - by El Forum - 12-08-2009, 12:13 PM
AJAX AND JSON - by El Forum - 12-08-2009, 04:47 PM
AJAX AND JSON - by El Forum - 12-17-2009, 01:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB