The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I have a basic php flatfile search script that I'm trying to modify.
The script works fine for visitors using a free form search but I would like to add some form checkboxes to make it friendlier.
The script uses a variable called 'keyword'.
The freeform search passes single or multiple search terms successfully but the FORM i'm attempting to use with checkboxes only makes use of the last selection.
The form I've created uses duplicate names - 'keyword'
<input type="checkbox" name="keyword"
value="hosting"
<input type="checkbox" name="keyword" value="broadband">broadband
In my ham fisted way I've read that I can change this to
"keyword[ ]"
and this should create an array. When I try this the script ends up searching for Array ! You can see the problem/code here:
www.hmmm.ip3.co.uk/searchtesting
Using FORM 1 searching for 'hosting broadband' only returns Freeola in the results.
Using FORM 2 selecting both hosting and broadband the script only searches for broadband - only picking up the last keyword.
Any help changing the FORM and/or script would be appreciated :¬)
Thanks.
[s]Hmmm...[/s]
hacked version
All seems to work but I need to separate the keywords.
At the moment selecting one from both selections puts them into one work.
Bet that's easy for you to fix!!! Not me though :¬(
Thanks again,
I'll be back in the morning as I'm already late...
Oh well, maybe I can explain without seeing. Insert this
$keyword='';
foreach($_POST as $key => $value) {
if (substr($key,0,7)=='search_') {
if (is_array($value)) {
$keyword.=' '.trim(implode(' ',$value));
} else {
$keyword.=' '.trim($value);
}
}
}
$keyword = trim(preg_replace('/[^a-zA-Z0-9s]/', '', $keyword));
after the line...
$filename = "$root/site.dat";
Change
if(strlen($_POST['keyword']) <= 1){
to
if(strlen($keyword) <= 1){
Finally delete...
$keyword = $_POST['keyword'];
if (is_array($keyword)) {
$keyword=implode(' ',$keyword);
}
$keyword = preg_replace('/[^a-zA-Z0-9s]/', '', $keyword);
Then name all your input /check / text boxes starting with "search_", like...
<form action="hmmm.php" method="post">
<input type ="hidden" name="action" value ="search">
<input type="radio" name="search_keyword1[]" value="hosting" checked>hosting�����
<input type="radio" name="search_keyword1[]" value="broadband">broadband�����
<input type="radio" name="search_keyword1[]" value="it">it����<p>
Would you like to add?<br>
<input type="radio" name="search_keyword2[]" value="bbc">bbc
<input type="radio" name="search_keyword2[]" value="help">help�����
<input type="radio" name="search_keyword2[]" value="stuff">stuff�����
<input type="radio" name="search_keyword2[]" value="blabla">blabla�����
<input type="submit" name="submit" value="Search" class="arial12"> <br>
</form>
Any field starting with search_ gets processed and appended to your search string.
> I think I understand. Don't use radio buttons in your form.
> They're not really designed for multiple selections. Use
> checkboxes or multi-option selects. If you are using checkboxes
> then users can select as many as they want.
> If you want only 1 option on each row selected then you can
> either use radios with each row having a different name attribute
> and then use php to loop through each in turn or us JS to stop
> more than one checkbox being selected (not foolproof I know).
Thanks for that.
I've tried some of that.
Checkboxes work but I need more than one mutually exclusive selection.
I can't change 'names' as you would normally do else it breaks my array using keyword[] which I need to keep to make the search work.
I've been trying JavaScript to make checkboxes behave like radio buttons linky but I don't think it likes having '[ ]' in the name!
[s]Hmmm...[/s]
If you want only 1 option on each row selected then you can either use radios with each row having a different name attribute and then use php to loop through each in turn or us JS to stop more than one checkbox being selected (not foolproof I know).
I thought Garin's help would allow me to do everything, but I've found more problems :¬(
I want my visitors to be able to search for more than one selection.
Using 'radio' buttons works fine using keyword[] but if I need more than one group it fails.
I've created a new page to show the problem:
www.hmmm.ip3.co.uk/searchtesting/more.php
I can't change the 'name' keyword[] or it will break everything!
I'm not sure if I've explained this properly - if you need more info please let me know.
Thanks again.
[s]Hmmm...[/s]
I've left my original page broken and the 'garin' link is working as required.
Thanks Garin - that's just what I wanted!
Very impressive - complete double-dutch to me...
[s]Hmmm...[/s]
brb