Jess
Jess

Reputation: 8700

CodeIgniter encode string through uri segment?

I am new to codeigniter, I am adding a search function to the site, and had two questions.

  1. How would I submit a form so that it would be sent as uri segments (like a query string)? I did it before by submitting with post, and redirecting to a url with it in the uri segments. 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? I tried urlencode, but there were still characters that weren't allowed. I want to keep the bulk of what the query is (so it is easily found in say history, so no base64_encode). Thoughts? Is there a built-in function for this or something?

Thanks for any help, Max

Upvotes: 0

Views: 3369

Answers (3)

Mark Eirich
Mark Eirich

Reputation: 10114

You would use JavaScript to form the URL, like this:

<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. However, from my experience with version 1.7, if you pass more than one GET parameter, you can retrieve them using the $_REQUEST array. This will bypass the URI filter altogether. 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. Hopefully this will work for you.

Upvotes: 3

Vamsi Krishna B
Vamsi Krishna B

Reputation: 11490

if you need to use something like

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

instead of CI's default URI Segments

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 .

Upvotes: 0

Francois Deschenes
Francois Deschenes

Reputation: 24969

  1. Yes. Create a form using 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:

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

Upvotes: 0

Related Questions