0

在 codeigniter 中,在/categories页面上,我有一个表格,其中包含所有类别项目的行。有一个<select>框可以按组件过滤类别:

<form accept-charset="utf-8" method="post" action="/categories/get_categories">
    <select onchange="this.form.submit()" name="selectCatsByComponent">
        <option value="0" selected="selected">-- Choose a component --</option>
        <option value="1">Content</option>
        <option value="2">E-commerce</option>
    </select>
</form>

因此,每当我<option><select>列表中选择一个并单击按钮ADD NEW CATEGORY时,我想将该 POST 值传递到下一页并自动在同一<select>列表中选择相应的组件 ID。

我试过这个,但它似乎不起作用:

    if( $this->input->post('selectCatsByComponent') )
        $com_id = $this->input->post('selectCatsByComponent');

有小费吗 ?

======= 更新 =======

伙计们,对于那些仍在寻找解决方案的人 - 在 GitHub 上查看我的模板库:

https://github.com/danieltorscho/CI_Template_lib

它做你需要的,没有,更少,没有更多。

4

1 回答 1

1

我猜选项必须在写入选择时使用检查。

<?php
/* Use a default, and try to get the value of the previous selection. */
$com_id = 0;
if( $this->input->post('selectCatsByComponent') )
    $com_id = $this->input->post('selectCatsByComponent');

$catsByComponentOptions = Array('-- Choose a component --','Content', 'E-commerce');
?>

/* start the form */
<form accept-charset="utf-8" method="post" action="/categories/get_categories">
    <select onchange="this.form.submit()" name="selectCatsByComponent">

<?php 
/* write out each option, checking to see if it needs to be selected. */
foreach ($catsByComponentOptions as $key => $value){
    echo '<option value="'. $key .'" ';
    if ($key === '$com_id')
        echo ' selected="selected" '; 
    echo ">$value</option>";
}
?>

</form>
于 2013-05-19T03:11:41.573 回答