Welcome Guest, Not a member yet? Register   Sign In
Search function: How to get form field values as CI URI parameters?
#1

[eluser]developer10[/eluser]
In the case the title's too confusing, here's what i need (im sure many others have the same problem):

I have a simple search form with only 2 fields (category and city).

In my form's action i put "show/something" and it takes me there after submitting.

Now, i want it to take me to "show/something/category/cars/city/sarajevo" for example (or whatever user selected in the two dropdown fields),
because then everything would work - i'd be able to get the results.

How do i tell it to append form's post values to the end of form's action value?

NOTE: i dont want to use querystrings and am pretty sure this can be done without them.

THANKS!
#2

[eluser]WebbHelp[/eluser]
Sorry if I'm wrong, but do you mean to send the formdata with GET instead of POST?
If you use GET it should appear in the adressfields as segments!
#3

[eluser]developer10[/eluser]
[quote author="webbhelp" date="1260747864"]Sorry if I'm wrong, but do you mean to send the formdata with GET instead of POST?
If you use GET it should appear in the adressfields as segments![/quote]

i'm not very experienced with CI yet

since i'm utilizing the form helper, POST is automatically chosen as the method. so, the question is how to
change this in order to have GET instead (if thats what will enable me to have those values as segments in my URI)
#4

[eluser]WebbHelp[/eluser]
I think I am in the same level as you when we are talking about codeigniter hehe Tongue

I suppose you use the form_helper() http://ellislab.com/codeigniter/user-gui...elper.html
I checked if there was anyway to choose get, but I didn't find it, but of course you can write like this:
<form method="get" action="http:/example.com/index.php/email/send" /> because you can mix "standard" html/php and codeigniters classes.
The question is; if you use get and it send with segments or like ?q=sdsd&j=sdsd
I don't know, try, just create a form and:
Code:
<form method="get" action="http:/example.com/index.php/email/send" />
<input type="text" name="field" value="sjdojsdo josjd os" />
<input type="submit" name="sub" value="Send" />
</form>
#5

[eluser]bretticus[/eluser]
You can still use POST, just build your action attribute dynamically based on the previous responses.

Code:
// load helper in controller
$this->load->helper('form');

// in your view, build your path dynamically.

$category = $this->uri->segment('4);
echo form_open('show/something/' . $category . '/'  . $this->input->post('city'));

This is a very "loose" example. Obviously you'd validate the post data to make sure it matches yours.

Another suggestion would be to look at "bread crumb" code because this is similar to what you are trying to accomplish (or so it seems.)
#6

[eluser]developer10[/eluser]
[quote author="bretticus" date="1260751944"]You can still use POST, just build your action attribute dynamically based on the previous responses.

Code:
// load helper in controller
$this->load->helper('form');

// in your view, build your path dynamically.

$category = $this->uri->segment('4);
echo form_open('show/something/' . $category . '/'  . $this->input->post('city'));

This is a very "loose" example. Obviously you'd validate the post data to make sure it matches yours.

Another suggestion would be to look at "bread crumb" code because this is similar to what you are trying to accomplish (or so it seems.)[/quote]

well, what if someone wants to search right from the homepage, when there's no uri segments involved (my form is in the header, so it is available
anytime user wants to search)
#7

[eluser]developer10[/eluser]
[quote author="webbhelp" date="1260748510"]I think I am in the same level as you when we are talking about codeigniter hehe Tongue

I suppose you use the form_helper() http://ellislab.com/codeigniter/user-gui...elper.html
I checked if there was anyway to choose get, but I didn't find it, but of course you can write like this:
<form method="get" action="http:/example.com/index.php/email/send" /> because you can mix "standard" html/php and codeigniters classes.
The question is; if you use get and it send with segments or like ?q=sdsd&j=sdsd
I don't know, try, just create a form and:
Code:
<form method="get" action="http:/example.com/index.php/email/send" />
<input type="text" name="field" value="sjdojsdo josjd os" />
<input type="submit" name="sub" value="Send" />
</form>
[/quote]

since i'm actually rebuilding my existing site into CI application, of course i know i can make it using pure HTML.
and to answer the question regarding querystrings - no, i dont want my URI end up looking like this: example.com/show/something?category=selectedCategory&city=selectedCity
i want it to be example.com/show/something/category/selectedCategory/city/selectedCity instead.

i have a feeling it IS possible to accomplish that !
#8

[eluser]Jondolar[/eluser]
Is there a specific reason you are doing this? It might be better to save the search fields in a session instead of trying to use the URL to save the state.
#9

[eluser]developer10[/eluser]
[quote author="Jondolar" date="1260771467"]Is there a specific reason you are doing this? It might be better to save the search fields in a session instead of trying to use the URL to save the state.[/quote]

i think it is not very important to have those values as segments

can you tell me how to store them in session and then use it, just like i'd use segments
#10

[eluser]pickupman[/eluser]
Using the Session class library:
Code:
$this->session->set_userdata('search', $this->input->post('field')); //Set search value in seesion
$this->session->userdata('search'); //Return search value from session

As you see in the documentation, you can also pass an associative array to the function. You could set multiple fields/search criteria. You will find this method an advantage when your results may yield more than one page of results, as $_POST variable will be available only on the next page requested.

A possible solution to keep variables in the parameter would be using a redirect:
Code:
if($this->form_validation->run())
    {
        $search  = $this->session->userdata('search');
        $city = $this->session->userdata('city');
        redirect("search/$search/$city");
    }




Theme © iAndrew 2016 - Forum software by © MyBB