简体   繁体   English

处理功能后重定向用户

[英]Redirect user once function processed

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. 目前,我只是看到一个空白屏幕,其url指向表单位置additem / insert,尽管一切正常。 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");
    }

}

It depends wether or not you want to go to a new controller once the form is filled out. 填写表单后,是否要转到新的控制器取决于您。 If not, I would do something like that : 如果没有,我会做这样的事情:

function insert()
{

    //Load the appropriate files (as the form ..) ..
    $this->load->library('form_validation');

    //Put some form_validetaion rules .. you have to set it for each field
    $this->form_validation->set_rules('title', 'title', 'trim|required|xss_clean');

    if(!$this->form_validation->run())
    {
        // the there was an error in the form or the user didn't submit it yet
        //load your view and pass an error message, see the form validation page for that
        $this->session->set_flashdata('errorMessage', 'see the error message for each field');
        $this->load->view('form', $data);
    }else{
        //YOUR CODE HERE
        // the form hass successfully validated
        $this->session->set_flashdata('flashMessage', 'The has been validated successfully');
        redirect('"controller"/"function"');

    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM