重定向并设置选择问题(_S)


Redirect and set_select problem

我使用的是CodeIgniter 1.7.1。好的,这是场景。当提交表格时,我需要做两件事

1) 保持下拉列表中选择的值。2) 使用session->set_flashdata(),我需要设置自定义数据库消息。

现在,正如我们所知,我们需要在设置这些闪存数据之前进行重定向。

这是我写的代码。

if ($this->form_validation->run() == TRUE){
    $this->session->set_flashdata(‘msg’, ‘Taha Hasan’); 
    redirect(current_url());
    $this->ShowReceiveInventoryView();
}

此外,我还在下拉视图中使用set_select来保持该值。

<select name=“myselect”>
<option value=“one” <?php echo set_select(‘myselect’, ‘one’, TRUE); ?> >One</option>
<option value=“two” <?php echo set_select(‘myselect’, ‘two’); ?> >Two</option>
<option value=“three” <?php echo set_select(‘myselect’, ‘three’); ?> >Three</option>
</select>

现在问题来了…闪烁消息出现了,但因为我正在重定向到当前页面,所以下拉set_select值丢失了!!!默认值出现在选择中:(..如果我删除代码中的重定向行,下拉值将预先列出,但未设置Flash数据!!

希望你们能解决这个问题…

set_select()仅在$_POST数组包含内容(正如您所发现的)时工作,但您的重定向显然是一个GET请求。

处理此问题的正确方法是在控制器中执行查询,将正在编辑的对象传递到视图中。然后,在视图中,根据$_POST(如果存在)或传递的对象重新填充窗体或设置默认值。

让我们假设我们正在编辑一个产品,它具有myselect(一个糟糕命名的字段)属性。我们将使用PHP的三元运算符来测试产品的myselect参数的值是否等于当前的option——如果是,我们将使用set_selects()的第三个参数来设置默认值。

<option value="one" <?php echo set_select('myslect', 'one', ((!$product) || !$this->input->post('myselect') && $product->myselect == 'one' ? TRUE : FALSE); ?>One</option>
<option value="two" <?php echo set_select('myselect', 'two', (!$this->input->post('myselect') && $product->myselect == 'two' ? TRUE : FALSE); ?>Two</option>