开发者

Codeigniter: Pass form variable into URI

开发者 https://www.devze.com 2022-12-15 20:36 出处:网络
Not sure if this can be done but it seems my main issue is because i have a default route to a method called \"index.\" I want to be able to list all users tagged with a specific keyword. In addition,

Not sure if this can be done but it seems my main issue is because i have a default route to a method called "index." I want to be able to list all users tagged with a specific keyword. In addition, users can search for other users based on these keywords.

i.e. www.domain.com/tags/apples www.domain.com/tags/oranges www.domain.com/tags/blueberry

It works fine if I go to the URL manually. I'm having issues getting it to work with a form field.

Snippet of the form_open:

<?=form_open('tags/');?>

<p>Search for Tag: <input type="text" name="tag" /></p>    
<p><input type="submit" value="Search" /></p> 

Here's a snippet of my controller:

function index() {
    $da开发者_Go百科ta['result'] = $this->tags_model->searchByTag($this->uri->segment(2));
    $this->load->view('tags_view', $data);
} 

Here's a snippet of my router:

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

Now, I can easily fix all this if I have a method called search, but I don't want the URL to show up as www.domain.com/tags/search/orange.


When you create your form you set it to use POST variables instead of GET, that way they don't go through the url, that's codeigniter's default method for forms.

So your form_open code will generate the following code:

 <form method="post" action="tags/" />

If you want them to got through url though, call the form opener this way instead:

form_open('tags/', array('method' => 'get'));

The same applies to any other attributes you want to specify for the form, just follow the same pattern attribute_name => attribute_value inside the array.

More info on the user guide


The problem here is that your form will be submitting all it's data to "/tags", with nothing trailing it, as POST data doesn't come in as part of the URL. Even if it was a GET request however, I don't think that CodeIgniter will take anything out of the querystring and use it as part of the routing segments.

I think what you should do is have a small Javascript function that automatically updates the form action parameter to be tags/<select option value> whenever the select value is changed. This way it will submit to the right place. In order to handle non-javascript enabled browsers, you could have a default action called tags/search that would simply analyze your form data and put out a 301 redirect to the proper tags/<location> once you'd figured it out.

It seems like a bit of overkill here however, as you could really point the form at tags/index and not worry about it. I'm not sure search engines index form submission locations, and even if they did, they certainly wouldn't index a form that submits to dynamic URIs in the way that you want it to. You could still link to the search result pages using tags/apples, etc, but the form could work quite normally just by going to tags/index.


I ended up redirecting the URL and passed the keyword into the URI.

i.e. domain.com/tags/view/

0

精彩评论

暂无评论...
验证码 换一张
取 消