一旦处理完功能,重定向用户

时间:2012-09-27 16:20:45

标签: php codeigniter

我是代码点火器的新手,我想知道如何将用户重定向回上一个视图,并在提交表单后显示成功或错误消息。

这是我的观点

<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>

这是我的控制器

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);
}

}

这是模型

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'].'');
    }
}

目前,我刚刚看到一个空白屏幕,其中url指向表单位置additem / insert,尽管一切正常。我只是想通过成功或失败消息将用户重定向回上一个视图。

2 个答案:

答案 0 :(得分:0)

好吧,我想你正试图搜索这样的东西:

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;
}

现在,根据交易状态,您可以显示相应的视图:

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");
    }

}

答案 1 :(得分:0)

表单填写后,您是否想要转到新的控制器。 如果没有,我会做那样的事情:

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"');

    }


}