【问题标题】:Add string from form input to URL using Codeigniter使用 Codeigniter 将表单输入中的字符串添加到 URL
【发布时间】:2011-08-22 04:40:03
【问题描述】:

我有一个包含搜索框的 div:

<div id="searchbox">
    <?php echo form_open('places/search/sort_by/id/sort_order/desc/term/'.$this->input->post('term')); ?>
        <?php echo form_label('Search for', 'term'); ?>
        <?php echo form_input('term', set_value('term'), 'id="term"'); ?>
        <?php echo form_submit('submit', 'Search'); ?>
    <?php echo form_close(); ?>
</div>

在输入中,用户可以输入一个搜索词,它将使用 $this->input->post('term') 将其传递给控制器​​,然后传递给模型以查询数据库。现在在输入搜索词并提交表单后,我希望 url 以搜索词结尾,即。如果 'dinosaur' 是搜索词,则 places/search/sort_by/id/sort_order/desc/term/dinosaur。我该怎么做?

现在上面的代码将 POST 传递给控制器​​,但提交表单后的 url 只是places/search/sort_by/id/sort_order/desc/term/。然后,如果用户搜索另一个词说“猫”,则在提交 url 后,当搜索词实际上是“猫”时,该 URL 将变为 places/search/sort_by/id/sort_order/desc/term/dinosaur。

我该如何解决这个问题?

换句话说,这就是我想要的行为

  1. 网址是 places/search/sort_by/id/sort_order/desc/term/ 搜索框为空
  2. 在搜索框中输入“恐龙”,然后按提交
  3. 网址现在是 places/search/sort_by/id/sort_order/desc/term/dinosaur 并且搜索框包含“恐龙”
  4. 在搜索框中输入'cats',然后按提交
  5. 网址现在是 places/search/sort_by/id/sort_order/desc/term/cats 并且搜索框包含“猫”

这就是我使用上述代码得到的结果

  1. 网址是 places/search/sort_by/id/sort_order/desc/term/ 搜索框为空
  2. 在搜索框中输入“恐龙”,然后按提交
  3. 网址现在是 places/search/sort_by/id/sort_order/desc/term/ 并且搜索框包含“恐龙”

  4. 在搜索框中输入'cats',然后按提交

  5. 网址现在是 places/search/sort_by/id/sort_order/desc/term/dinosaur 并且搜索框包含“猫”

【问题讨论】:

    标签: php mysql forms codeigniter


    【解决方案1】:

    试试这个...

    <div id="searchbox">
        <?php echo form_open('places/search/sort_by/id/sort_order/desc/term/'); ?>
            <?php echo form_label('Search for', 'term'); ?>
            <?php echo form_input('term', set_value('term', $term), 'id="term"'); ?>
            <?php echo form_submit('submit', 'Search'); ?>
        <?php echo form_close(); ?>
    </div>
    

    然后在接受它的控制器中...

                          // Note the default NULL value for the last uri segment
                         //                                                   |
                        //                                                    v
    function search($sort_by, $id, $sort_order, $desc, $term, $search_term = NULL)
    {
         // if $search_term is null redirect on itself concating the "post" data 
        // on the end of the uri
        if ($search_term === NULL) 
        { 
            redirect('the/same/uri/plus/the/term/'.$this->input->post('term'));
        }
    
        // load up this variable to fill the input
        $data['term'] = $search_term;
    
        //then do whatever
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多