提交表格2后,表格1中的Codeigniter后值发生变化

时间:2013-11-05 20:54:30

标签: php forms codeigniter post

我最终使用会话数组并在那里存储数据,以便稍后再次引用它。我刚刚将每个表单中的帖子数据添加到此数组中,稍后在我的else块中引用它。感谢您的帮助!

我正在将CodeIgniter用于学校项目。我有一些PHP的经验,但对这个框架来说相对较新。我在一个页面上使用两个表单时遇到问题。

我有一个表单显示艺术家的下拉列表。单击此表单的提交按钮后,它会更新同一页面上的第二个表单(另一个下拉列表),其中包含属于第一个下拉列表中所选艺术家的作品集。

我正在尝试回显每个表单中的值,仅用于测试目的,稍后我将实现其他功能。我的问题是,在提交第二个表单后,第一个表单的帖子值会发生变化。如果我在提交第二个表单之前回显第一个表单的选定值,则会显示所选的值。如果我在提交两个表单后回显第一个表单的值,它会显示该下拉列表中的第一个可用值。

我需要能够从这两个表单中获取值,然后在提交两个表单后再使用它们。因此,当我需要使用它们时,我不能正确地改变这些值,显然,任何帮助都会受到赞赏。非常感谢。

控制器

public function formtest(){

    //Making a call to the model to get an array of artists from the DB
    $data = $this->depot_model->get_artists_list();

    $this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
    $this->form_validation->set_rules('ports', 'Portfolios', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        //Building the artists dropdown form
        $data['data'] = form_open('formtest', 'class="superform"')
        . form_label('Artist<br/>', 'artist')
        . form_dropdown('artist', $data)
        . form_submit('mysubmit', 'Submit')
        . form_close();

        //Setting up a temp array of the selected artist's portfolios
        $ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));

        //Culling out the names of the portfolios from my temp array
        $newdata = array();
        foreach($ports as $port){array_push($newdata, $port['name']);}

        //Building the artist's portfolio dropdown
        $newdata['data'] = form_open('formtest', 'class="superform"')
        . form_label('Portfolios<br/>', 'ports')
        . form_dropdown('ports', $newdata)
        . form_submit('mysubmit', 'Submit')
        . form_close();

        //Send the information to my view
        $this->load->view('formtest', $data);
        $this->load->view('formtest', $newdata);


    }
    else
    {
        //This echos out the first available value from my dropdown rather than the one I selected.
        echo $data[$this->input->post('artist')];
        echo "success";
    }

}

1 个答案:

答案 0 :(得分:0)

表单是分开的,因此当提交第二个表单时,实际上从第一个表单收到 no 值,因为它不包含在第二个表单中的字段中。所以你可以这样做,包括说出具有艺术家价值的第二种形式的隐藏字段。例如:

$newdata['data'] = form_open(
    'formtest', 
    'class="superform"', 
    array('artist' => $this->input->post('artist'))
);