Welcome Guest, Not a member yet? Register   Sign In
Passing field value to URL
#1

[eluser]sdotsen[/eluser]
Ultimately I would like the following:

www.domain.com/tags/orange
www.domain.com/tags/apple

One can view all users tagged w/ a specific word by inputting the URL (shown above) or searching for it via a search box. The code works if I type in the URL by hand, but ultimately I would like to put a search field and have the user search for a tag.

Let's say I have the following code to display the search box on one of my pages. Is it possible to pass the "tag" field value into the URL?

Code:
<?=form_open('tags/');?>
    
<p>Search for Tag: &lt;input type="text" name="tag" /&gt;&lt;/p>    
<p>&lt;input type="submit" value="Search" /&gt;&lt;/p>

Now the code for my "tags" class

Controller (snippet):

Code:
function index() {
    
    $data['result'] = $this->tags_model->searchByTag($this->uri->segment(2));
    
    $this->load->view('tags_view', $data);

}

Model (snippet):

Code:
function searchByTag($tag) {

    $m = new Mongo();
    $collection = $m->selectDB( "$this->db_name" )->selectCollection( "profiles" );
    
    $data = $collection->find(array("tags" => $tag) );

    return $data;
}

Routes:
Code:
$route['tags'] = "tags/index";
$route['tags/(:any)'] = "tags/index/$1";
#2

[eluser]Sbioko[/eluser]
Add the id attribute to your input tag and remove form_open function. And on onsubmit event get the field's value and redirect the page. I will not provide you full code, because ci filters HTML.
#3

[eluser]brianw1975[/eluser]
if it weren't apparent, what's basically being said above is this: don't use a regular form submit, use javascript to grab the text from the input field and use it to do a location(?).href change using javascript.

Personally, I would do a regular submit, have the code see if there are any tags at all that match and if so then use a redirect to the proper url, if no matching tags then provide some other content rather than relying on the javascript to always work.




Theme © iAndrew 2016 - Forum software by © MyBB