Redirect the user after the function has been processed

advertisements

I'm new to code igniter and I'm wondering how I can redirect a user back to the previous view and display a success or error message after they have submitted a form.

Here is my view

<div id="modal">
    <?php echo form_open('additem/insert', array('id'=>'insert')); ?>
    <div class="field">
        <label for="title">Title:</label>
        <input type="text" name="title" value="<?=(isset($_GET['title'])) ? $_GET['title'] : ''?>" />
    </div>
    <div class="field">
        <label for="link">Link:</label>
        <input type="text" name="raw_link" value="<?=(isset($_GET['link'])) ? $_GET['link'] : ''?>" />
    </div>
    <div class="field">
        <label for="title">Additional Info: <span class="tip">(size or colour)</span></label>
        <input type="text" name="additional_info" />
        <input type="hidden" name="fb_user_id" value="<?=$_SESSION['data']['fb_user_id']?>" />
    </div>
</form>

Here is my controller

class Additem extends CI_Controller {
public function index() {

    $this->load->helper('form');
    $this->load->model('list_model');

    $this->load->view('modal/add-item');
}

public function insert() {

    $data = array(
        'id' => $this->input->post('id', TRUE),
        'title' => $this->input->post('title', TRUE),
        'raw_link' => $this->input->post('raw_link', TRUE),
        'additional_info' => $this->input->post('additional_info', TRUE),
        'fb_user_id' => $this->input->post('fb_user_id', TRUE)
    );

    $this->load->model('list_model');
    $this->list_model->add_list_item($data);
}

}

Here is model

function add_list_item($data) {

    $this->load->database();

    $this->db->trans_start();

    $this->db->query("INSERT list_items (fb_user_id, added, title, raw_link, additional_info) VALUES(".$this->db->escape($data['fb_user_id']).", NOW(), ".$this->db->escape($data['title']).", ".$this->db->escape($data['raw_link']).", ".$this->db->escape($data['additional_info']).")");
    $this->db->query("UPDATE users SET num_items = num_items +1 WHERE fb_user_id = ".$this->db->escape($data['fb_user_id'])."");

    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE) {
       log_message('error', 'Unable to add list item for user '.$data['fb_user_id'].'');
    }
}

At the moment I am just presented with a blank screen with the url directing at the form location additem/insert, although everything works as should. I just want to redirect the user back to the previous view with a success or fail message.


Well, I think you are trying to search for something like this:

function add_list_item($data) {

    $this->load->database();

    $this->db->trans_start();

    $this->db->query("INSERT list_items (fb_user_id, added, title, raw_link, additional_info) VALUES(".$this->db->escape($data['fb_user_id']).", NOW(), ".$this->db->escape($data['title']).", ".$this->db->escape($data['raw_link']).", ".$this->db->escape($data['additional_info']).")");
    $this->db->query("UPDATE users SET num_items = num_items +1 WHERE fb_user_id = ".$this->db->escape($data['fb_user_id'])."");

    $this->db->trans_complete();

    $status = TRUE;
    if ($this->db->trans_status() === FALSE) {
       log_message('error', 'Unable to add list item for user '.$data['fb_user_id'].'');
       $status = FALSE;
    }
    return $status;
}

Now, according to transaction status you can show the respective view:

public function insert() {

    $data = array(
        'id' => $this->input->post('id', TRUE),
        'title' => $this->input->post('title', TRUE),
        'raw_link' => $this->input->post('raw_link', TRUE),
        'additional_info' => $this->input->post('additional_info', TRUE),
        'fb_user_id' => $this->input->post('fb_user_id', TRUE)
    );

    $this->load->model('list_model');

    $status = $this->list_model->add_list_item($data);
    if ($status == TRUE)
    {
        // 1. you can show a view
        $this->load->view('success_view');

        // 2. or do a redirection
        redirect('success_controller', "refresh");
    }
    else
    {
         // 1. you can show a view
        $this->load->view('error_view');

        // 2. or do a redirection
        redirect('error_controller', "refresh");
    }

}