dongliang7545 2013-05-18 21:05
浏览 51
已采纳

在CodeIgniter中将变量从一个页面传递到另一个页面

In codeigniter, on the page /categories I have a table with the rows of all category items. There is a <select> box to filter the categories by components:

<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>

So whenever i select an <option> from the <select> list , and I click on the button ADD NEW CATEGORY, i want to pass that POST value into the next page and automatically select the corresponding component id in the same <select> list.

I tried this but it seems not working:

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

Any tips ?

======= UPDATE =======

Guys, for those who are still in searching for a solution - check out my Template Library on GitHub:

https://github.com/danieltorscho/CI_Template_lib

it does what you need, nothing, less, nothing more.

  • 写回答

1条回答 默认 最新

  • dongyanling9248 2013-05-19 03:11
    关注

    I guess the options have to use a check when the select is written.

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮