Question

I have tried my best to search for an existing question with smiliar issue but was not able to find one. Here's my situation

I have a controller named search which accepts a parameter "search term", when this controller is called directly from URL like www.xyz.com/search/red+car it returns results and the URL in browser address bar is www.xyz.com/search/red+car but when the user submits the search from a from in webpage with same term, the results are coming fine but the URL does not reflect the search term.

If I do a redirect the form POST data is lost, although resubmitting the POSt is not a solution in my case either. I need a way to change the URL so that it shows the search term.

Thanks in advance for your help, please be gentle as this is my first question.

@Vlakarados - I am trying to use same controller to provide search results from post data and controller parameter. Both work fine but when using search form on webpage the search parameter is not reflected in URL.

@Rakesh Shetty - The actual method is very long, but here is the compressed format build query as per post data and passed parameter. populate view with results render the view

Was it helpful?

Solution

Thanks everyone for suggesting various solution.

I used jquery to change the form action parameter, now when ever the dropdown selection is changed the value is appended to the action parameter. I also took out the dropdown from being part of post data.

There are 2 dropdowns one for area and other for zip code. This new approach works with bot dropdowns and my exsting code of controller works without any major change.

I used the following javascript code to create new action parameter when ever user changes the area. Same goes for ZIP code.

$("#cityname").change(function(){
    var action = $(this).val();
    alert(action);
    $("#searchform").attr("action", "search/" + action);
});

I think I was not able to clearly explain my situation otherwise you people would have suggested this long ago.

OTHER TIPS

The better way to do it is in your controller:

function search($search_query) {

    // .. use your query as you normally would - display products, posts, messages
    $data = array('search_query' => $search_query);
    $this->load->view('search', $data);
}

// use this method as the form action
function search_proxy() {
    $search_query = $this->input->post('search');
    // if needed urlencode or other search query manipulation
    redirect('controller/search/'.$search_query);
}

This way you will have no problems and will only work with data in your url.

Edit: The second option is yo use JavaScript - when user submit's the form, change the form's action to include the searched query in form action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top