0

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

这是我的看法

<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,尽管一切正常。我只想通过成功或失败消息将用户重定向回上一个视图。

4

2 回答 2

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

}
于 2012-09-27T17:07:08.423 回答
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"');

    }


}
于 2012-09-27T17:16:47.420 回答