0
votes

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

3
I'm not quite sure what your question is. Do you want to submit a GET request from PHP or do you want PHP to handle a GET request? Can you explain how the user would generate the search query? PHP is server side so it can't be "user-generated". If you could elaborate on what you're trying to do and perhaps include some example of what you've done so it's clearer. - Francois Deschenes
I rephrased it as best I could, does that make sense? If not, I'll just rewrite it. - Ben
Do you have multiple fields on your search form, or just a single search box? - Mark Eirich

3 Answers

3
votes

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.

0
votes
  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'));
0
votes

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 .