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

[eluser]Daem0nX[/eluser]
I implemented the following for my search / results to use query strings, however I believe the same could apply here for segmented urls. Please note, I typed this on the fly and haven't tested the code, its merely an example and may need some modifications for your use.
Code:
form_open('/search/pre_search');

function pre_search() {
$query = '';
foreach ($_POST as $key => $value) {
if ($value != '') {
$query .= $key."/".$value."/";
} else {
//value is blank, set to 0 so you don't get a segment with $key//$key/$value
$query .= $key."/0/";
}
redirect('/search/results/'.$query);
}
}

function results() {
//parse url segments and show results
}
If you searched for category 'items' and city 'seattle', you would in the end be pushed to site.com/search/results/category/items/city/seattle/

--Edit--
Didn't read entire thread, post above outlined redirect. Differences with my code is it will allow for more fields (array of POST) and the redirect has slightly different segment info.
#12

[eluser]developer10[/eluser]
[quote author="Daem0nX" date="1260802818"]I implemented the following for my search / results to use query strings, however I believe the same could apply here for segmented urls. Please note, I typed this on the fly and haven't tested the code, its merely an example and may need some modifications for your use.
Code:
form_open('/search/pre_search');

function pre_search() {
$query = '';
foreach ($_POST as $key => $value) {
if ($value != '') {
$query .= $key."/".$value."/";
} else {
//value is blank, set to 0 so you don't get a segment with $key//$key/$value
$query .= $key."/0/";
}
redirect('/search/results/'.$query);
}
}

function results() {
//parse url segments and show results
}
If you searched for category 'items' and city 'seattle', you would in the end be pushed to site.com/search/results/category/items/city/seattle/

--Edit--
Didn't read entire thread, post above outlined redirect. Differences with my code is it will allow for more fields (array of POST) and the redirect has slightly different segment info.[/quote]


this thing works perfectly, just the i wanted it - with segments! i dont know why dont you also use segments, rather than querystrings

im also wondering why do people bothering with storing everything in session (the other suggestion, which i would definitely try if this didnt work).
maybe some of the resons are this redirecting, or maybe they all use it in case when they want their data to be POST (ie, invisible to the user).

or, maybe there are some other benefits and advantages of using sessions when deaing with such issues
#13

[eluser]pickupman[/eluser]
One of the reasons for using a session instead of URI segments is session can't be changed by the user. Either application make sure you are cleaning/scrubbing the segments, as there maybe disallowed characters. Search was "I used a space".

Try using:
Code:
$str = ''
foreach($_POST as $key=>$value)
{
    $str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : '';
}
redirect('search/results'.$str);
#14

[eluser]jcavard[/eluser]
[quote author="cold_fusion" date="1260748112"]
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)[/quote]

If what he wants is the form to be transmitted using $_GET, wouldn't this be a whole bunch easier?
Code:
echo form_open('email/send', array('method' => 'GET'));
#15

[eluser]developer10[/eluser]
[quote author="jcavard" date="1260842732"][quote author="cold_fusion" date="1260748112"]
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)[/quote]

If what he wants is the form to be transmitted using $_GET, wouldn't this be a whole bunch easier?
Code:
echo form_open('email/send', array('method' => 'GET'));
[/quote]

one of the above suggestions works for me.

but still (regarding your suggestion), what if CI decides to assign both methods LOL (post by default, get as a given attribute - didnt check whether it is possible)
#16

[eluser]developer10[/eluser]
[quote author="pickupman" date="1260842474"]One of the reasons for using a session instead of URI segments is session can't be changed by the user. Either application make sure you are cleaning/scrubbing the segments, as there maybe disallowed characters.

Try using:
Code:
$str = ''
foreach($_POST as $key=>$value)
{
    $str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : '';
}
redirect('search/results'.$str);
[/quote]

sorry if i bother you, but i didnt quite understand your last post
if i urlencode my segments, what would i gain?

maybe it'd be better if you explained to me this line of code in plain english Smile

Code:
$str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : '';

EDIT: and what does this mean:
Quote:Search was "I used a space".
#17

[eluser]jcavard[/eluser]
[quote author="cold_fusion" date="1260843241"]
maybe it'd be better if you explained to me this line of code in plain english Smile

Code:
$str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : '';
[/quote]

urlencode() - this function will help prevent xss and sql injection. It also encodes all "&" into "&" to avoid conflicts. Later just used urldecode($encoded_value) to get the original value.


You will not likely "gain" anything. It will just make your app security stronger. As for the purpose of this, well it loops through each field submitted in the form, and if the value is not empty ($value != ''), it appends the fields and value to a string, that is later used to redirect.

The fact the $key and $value are urlencoded, prevent user from trying to pass in bad characters. HTH
#18

[eluser]pickupman[/eluser]
Quote:
Code:
$str = ''; //Set empty string
foreach($_POST as $key=>$value)
{
    $str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : ''; //ternary operator (test) ? if : else;

}
redirect('search/results'.$str);

The code loops through each $_POST and test if value is set. If the value is set, then add key/value to $str, else add nothing to $str. PHP.net Docs: Ternary Operators

Quote:sorry if i bother you, but i didnt quite understand your last post
if i urlencode my segments, what would i gain?
You would want encode your string so they are not broken by CI. Example if the search was "I used a space", then your code would return "search/result/I used a space". This is an invalid URI, and will through an error in CI.

Using urlencode would result result in
Code:
search/result/I20used20a20space

The forum is not parsing the string correctly for it to be displayed properly, but you would end up with a string with the space url encoded to the safe equivalent.

CodeIgniter Security Docs
#19

[eluser]developer10[/eluser]
[quote author="pickupman" date="1260844812"]
Quote:
Code:
$str = ''; //Set empty string
foreach($_POST as $key=>$value)
{
    $str .= ($value !='') ? '/'.urlencode($key).'/'.urlencode($value) : ''; //ternary operator (test) ? if : else;

}
redirect('search/results'.$str);

The code loops through each $_POST and test if value is set. If the value is set, then add key/value to $str, else add nothing to $str. PHP.net Docs: Ternary Operators

Quote:sorry if i bother you, but i didnt quite understand your last post
if i urlencode my segments, what would i gain?
You would want encode your string so they are not broken by CI. Example if the search was "I used a space", then your code would return "search/result/I used a space". This is an invalid URI, and will through an error in CI.

Using urlencode would result result in
Code:
search/result/I20used20a20space

The forum is not parsing the string correctly for it to be displayed properly, but you would end up with a string with the space url encoded to the safe equivalent.

CodeIgniter Security Docs[/quote]

OK, thanks

this code is going to be for the search form consisting of only two dropdown fields (and maybe some more fields with predefined options to choose from).
i think i dont need urlencode() for that?

now i have it working (displaying search results) just like my site's regular data presentation, since it is redirected to that address. this means my search function won't search whole database for
certain keywords - the whole point is to filter (narrow) all records by two main parameters - category and city.
#20

[eluser]pbreit[/eluser]
Pardon the newbie question but how can I implement a simple GET like this:
http://www.mysite.com/search?s=form+handling

I can't find any help on this basic topic and this thread seems to indicate this is not straightforward.




Theme © iAndrew 2016 - Forum software by © MyBB