我是代码点火器的新手,我想知道如何将用户重定向回以前的视图并在他们提交表单后显示成功或错误消息。
这是我的看法
<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,尽管一切正常。我只想通过成功或失败消息将用户重定向回上一个视图。