2

Hey guys, I've got a search bar and it looks fine, but i don't really know how to make it search the whole of my site... Here's my html code so far:

<form class="search2" method="get" action="default.html" />
<input class="search2" type="text" name="serach_bar" size="31" maxlength="255"       
value="" style="left: 396px; top: 153px; width: 293px; height: 26px;" />
<input class="search1" type="submit" name="submition" value="Search" style=" padding-  
bottom:20px; left: 691px; top: 153px; height: 23px" />
<input class="search2" type="hidden" name="sitesearch" value="default.html" />

Thanks in advance guys!

Sam
  • 43
  • 1
  • 1
  • 6
  • 3
    You will most likely need a server-side language to handle the actual searching - unless you're willing to use Google Site search! – Pekka Mar 05 '11 at 22:49
  • `window.location = 'http://google.com/?q=site:mysite.com+'+escape(document.getElementById('searchbar');` let google handle it, or go grab the SDK so you can integrate it in your website. – Brad Christie Mar 05 '11 at 22:51
  • I want my site to be independent so i'll think i'll pass on the google one! :L But i do think i'll need somekind of php script or something but im not that advanced in php... – Sam Mar 05 '11 at 22:55

1 Answers1

2

I know you said you want to skip the Google route, but in case you want an interim solution while you go down the path of writing your own code to search your site, this will help (I've expanded on what Brad Christie posted in his comment above):

Your HTML from above with element IDs added:

<form id="frmSearch" class="search2" method="get" action="default.html" />
<input class="search2" id="txtSearch" type="text" name="serach_bar" size="31" maxlength="255"       
value="" style="left: 396px; top: 153px; width: 293px; height: 26px;" />
<input class="search1" type="submit" name="submition" value="Search" style=" padding-  
bottom:20px; left: 691px; top: 153px; height: 23px" />
<input class="search2" type="hidden" name="sitesearch" value="default.html" />

The JavaScript:

<script type="text/javascript">
    document.getElementById('frmSearch').onsubmit = function() {
        window.location = 'http://www.google.com/search?q=site:yoursitename.com ' + document.getElementById('txtSearch').value;
        return false;
    }
</script>

Since the JS to power the search is not inside a window.onload function you will need to place the script block after your form. I've tried to simplify this as much as possible to help you get it integrated and working right away. Oh, and don't forget to change "yoursitename.com" in the above JS to your actual domain name.

Infotekka
  • 10,307
  • 2
  • 20
  • 17
  • what do you mean by an interm solution (sorry im kinda new!)? – Sam Mar 05 '11 at 23:16
  • Something to work in the meantime, while you build out your own search functionality – Infotekka Mar 05 '11 at 23:17
  • Ermm... well it's not actually accessable on the web yet i'm still building it and i'm not rushed so i think i'm gonna at least attempt to create my own... (but i'll probably fail!) – Sam Mar 05 '11 at 23:20
  • Ah, I see, in that case then this won't help you! Ha, well if it ever does become web accessible and Google crawls it, this will work like a charm. – Infotekka Mar 05 '11 at 23:21
  • Ok thanks for the help, do you know where i could start on building a php or something?? – Sam Mar 05 '11 at 23:22
  • Start building the ability to search your site using PHP? – Infotekka Mar 05 '11 at 23:24
  • You may want to post another question here on SO that reads something to the effect of "How do I create a search feature for my site using PHP?" – Infotekka Mar 05 '11 at 23:26
  • Yeah... like i know a little php because i've worked with python and perl and they are similar but i'm not really sure where to start... – Sam Mar 05 '11 at 23:27