简体   繁体   English

CodeIgniter通过uri段编码字符串?

[英]CodeIgniter encode string through uri segment?

I am new to codeigniter, I am adding a search function to the site, and had two questions. 我是Codeigniter的新手,正在向网站添加搜索功能,并且有两个问题。

  1. How would I submit a form so that it would be sent as uri segments (like a query string)? 我将如何提交表单,以便将其作为uri段发送(如查询字符串)? I did it before by submitting with post, and redirecting to a url with it in the uri segments. 我之前通过提交post并在uri段中将其重定向到url来做到这一点。 But is there a better way? 但是有更好的方法吗?
  2. How would I send a string (such as a search query, completely user-generated) through a uri segment? 如何通过uri段发送字符串(例如完全由用户生成的搜索查询)? I tried urlencode, but there were still characters that weren't allowed. 我尝试了urlencode,但仍有某些不允许的字符。 I want to keep the bulk of what the query is (so it is easily found in say history, so no base64_encode). 我想保留大部分查询内容(因此很容易在历史记录中找到它,所以没有base64_encode)。 Thoughts? 有什么想法吗? Is there a built-in function for this or something? 有内置的功能吗?

Thanks for any help, Max 谢谢您的帮助,麦克斯

You would use JavaScript to form the URL, like this: 您将使用JavaScript形成URL,如下所示:

<form onsubmit="window.location.href='/search/'+encodeURIComponent(document.getElementById('search_query').value);return false">
    <input id="search_query" type="text" />
    <input type="submit" />
</form>

EDIT: The above answer will not work because of CodeIgniter's URI filter. 编辑:上面的答案将由于CodeIgniter的URI过滤器而无法工作。 However, from my experience with version 1.7, if you pass more than one GET parameter, you can retrieve them using the $_REQUEST array. 但是,根据我对1.7版的经验,如果传递多个GET参数,则可以使用$ _REQUEST数组检索它们。 This will bypass the URI filter altogether. 这将完全绕过URI过滤器。 So do this: 这样做:

<form action="/search">
    <input name="x" type="hidden" />
    <input name="q" type="text" />
    <input type="submit" />
</form>

Then use $_REQUEST['q'] to get your search query. 然后使用$ _REQUEST ['q']进行搜索。 Hopefully this will work for you. 希望这对您有用。

  1. Yes. 是。 Create a form using method="get" . 使用method="get"创建一个表单。
  2. If setup your form as mentioned above, you won't have to worry about encoding the requests. 如果如上所述设置表单,则无需担心编码请求。

Example: 例:

<form action="example.php" method="get">
  <input type="text" name="search" />
  <input type="submit" value="Search" />
</form>

Update : To generate the form the "CodeIgniter" way: 更新 :以“ CodeIgniter”方式生成表单:

echo form_open('email/send', array('method' => 'get'));

if you need to use something like 如果您需要使用类似

index.php?c=search&m=search&q=wow

instead of CI's default URI Segments 而不是CI的默认URI段

you have to enable query strings . 您必须启用查询字符串。 you can do it by changing some configuration in 您可以通过在

 application/config.php

find

$config['enable_query_strings'] = FALSE;

and change it to 并将其更改为

$config['enable_query_strings'] = TRUE;

Then you will be able to use query strings . 然后,您将可以使用查询字符串。

but mots CI people prefer not to use query strings, I am not sure why . 但是mots CI人更喜欢不使用查询字符串,我不确定为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM