SID-00363: How to include webs by checkboxes in search form
| Status: |
Answered |
TWiki version: |
4.3.1 |
Perl version: |
5.10.0 |
| Category: |
CategorySearch |
Server OS: |
Windows XP Professional |
Last update: |
16 years ago |
The basic TWiki installation includes a search form which allows searching either the current web or all webs.
Is it possible to include a checkbox per web? This way, the user could narrow their search to just the webs checked.
--
DanKerr - 2009-06-11
Discussion and Answer
If I were coding this in another programming environment when the button was clicked, the parameter 'web' of SEARCH could be set by looping through each checkbox and adding it's value to the string if appropriate.
Is there a way to set a variable at the beginning of the page and then add to it when the user clicks "Search"?
--
DanKerr - 2009-06-11
Here are some hints on how I would approach this. It is possible to show a form with a checkbox per web. If you submit it you get multiple parameters such as
item=Foo;item=Bar etc, e.g. not what we want. We need a single parameter such as
web=Foo,Bar. You can achieve this by using some Javascript on focus loss of the checkboxes to populate a hidden input field named
web.
Here is the first part, the form with dynamic checkbox list:
Now you need to add some Javascript to populate the hidden form field named
"web".
For some additional inspiration study
WebChangesForAllWebs.
--
PeterThoeny - 2009-06-13
Wow! Thanks for your guidance on this Peter, I'll post what I come up with.
--
DanKerr - 2009-06-15
Many thanks to
PeterThoeny for the guidance on this issue. The code below is a search form including PT's checkbox weblist. It also includes a javascript function which loops through all the form's controls, checks for checkboxes and then adds their names to a variable which is then output as a hidden formfield called web.
It works great for us.
<script language="javascript" type="text/javascript">
function loopForm(form) {
var cbResults = '';
for (var i = 0; i < form.elements.length; i++ ) {
if (form.elements[i].type == 'checkbox') {
if (form.elements[i].checked == true) {
cbResults += form.elements[i].value + ', ';
}
}
}
// here we cut off the last ", "
cbResults = cbResults.substring(0, cbResults.length-2)
form.web.value = cbResults
}
</script>
<form name="MySearchForm" action="%SCRIPTURL{view}%/Main/WebSearchResults">
<p><strong>Search:</strong> <input type="text" class="twikiInputField" name="search" value="" size="50" /></p>
<p><strong>Included webs:</strong> </p>
<blockquote>
%WEBLIST{ "<input type='checkbox' value='$name' /> $name<br />" }%
</blockquote>
<input type="hidden" name="type" value="keyword">
<input type="hidden" name="scope" value="all">
<input type="hidden" name="excludetopic" value="Web*">
<input type="hidden" name="web">
<input type="submit" class="twikiSubmit" value="Search" onclick="loopForm(document.MySearchForm);">
</form>
--
DanKerr - 2009-06-15
Thanks for reporting back Dan. I imagine this mod could be useful to other sites (me: I just forced it to always search all webs).
I took the liberty of reformatting your javascript in rev 5 because ... it needed it.
--
SeanCMorgan - 2009-06-16
Your welcome Dan. And thanks Sean for following up.
BTW, start tagging content, it helps people find useful stuff.
--
PeterThoeny - 2009-06-16
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.