Elliot Swan

Welcome to my live redesign. Codename, Tumbl3. Grab the feed.

Wednesday (07/5/06)

Better-Than-Live AJAX WordPress Search 10:16 pm

Whew. That was a lot of buzzwords for one title, wasn’t it? Yeah, yeah, yeah, I know. But hey–if they fit the bill, I can’t do anything about that, can I?

Update: This script has been updated, and information on the newer version is available over here.

So let’s see it.

Fine. I first implemented it over at Accidental Procrastination, so you are welcome to go check it out there.

Update: I also have added it onto this site, so you can check it out here as well.

Why is it “better”?

Because I find the whole live search thing a little bit dumb. There’s a reason that Google Suggest never went out of beta: it’s annoying. It’s completely pointless to start searching when all I’ve typed is a b, wait until I’ve typed better-than-live, then search. I’ll tell you when I’m ready (I know where my enter key is, thank you very much).

Something that I can see as useful, however, as an AJAX search. You know, eliminate some of those page refreshes. Besides, it’s just so freaking cool.

Can I use it?

Why, of course. You can download the files over here, then continue reading as I tell you what they do and how to set this up.

First, we of course need to get ourselves a form:


<form method="get" id="search" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" value="Better-than-live search." name="s" id="search_input" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

We’ll also need a div to show the results in:

<div id="searchresults" style="display: none;"></div>

For that nifty little close button, we need our close link:


<a id="closeresults" href="#" title="Close search results." style="display: none;">Close</a>

Now to do the searching. I’ve included a file called search.php, here’s what is in it:


<?php
/* Make sure to replace "yourpath" with the path to your wordpress directory */
include("yourpath/wp-blog-header.php");
?>

<ul>
<?php if (have_posts()) : while (have_posts()) : the_post();  ?>
	<li><a title="Permalink to this post" href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; endif; ?>
<?php if (!have_posts()) { echo('<li>No results to show.</li>'); } ?>
</ul>

After filling in yourpath, upload this to the directory that your current theme is in. Right now our search is completely functional, just ugly. This is what people without JavaScript enabled will see (though, since you’ll probably style your results, they’ll of course see some nice styles).

Bring in the JavaScript

This JavaScript is using Prototype and Script.aculo.us, so you’ll first need to include those (they are included in the zip file).

Now open up search.js. You probably need to change line 45 to include the correct path of your indicator image. For my indicator image, I’m using the first one by Jakob Skjerning over at mentalized. Set the variable url in line 72 to the path to your WordPress index. So, if you have WP installed in a directory called “wordpress,” then have it something like http://www.mydomain.com/wordpress/index.php. Savvy?

Good. Now download the files, do some CSS stylin’, and have fun. If you want a place to start with your styles, feel free to take a peak at mine (just don’t hotlink to my images):


#search {
height: 17px;
border: 1px solid black;
background: white;
width: 200px;
position: relative;
}

#search input {
font: 13px georgia;
padding-left: 4px;
height: 16px;
letter-spacing: 1px;
border: none;
width: 179px;
}

#search #search_input {

float: left;
border: 0px solid green;;
}

#search input#searchsubmit {
/* ok2.gif available at http://bs-markup.de/micons/ */
background: white url(/accidental/wp-content/themes/worn/images/ok2.gif) 1px 0 no-repeat;
margin-left: -4px;
padding-top: 15px;
width: 17px;
display: block;
float: right;
margin-top: 1px;
height: 15px;
cursor: pointer;
overflow: hidden;
}

#search input#searchsubmit:focus {
outline: none;
}

#search img#indicator {
height: 16px;
width: 17px;
}

#search img#indicator-safari {
position: absolute;
height: 16px;
width: 17px;
top: 1px;
right: -20px;
}

* html #search img#indicator {
margin-left: -4px;
}

#searchresults {
clear: both;
}

#searchresults ul {
margin: 0;
position: relative;
top: 10px;
padding: 30px;
list-style: none;
background: #191C1D;
}

#searchresults li, #searchresults a {
color: #FFF8BF;
}

#searchresults a:hover {
color: #3C4918;
}

#closeresults {
display: block;
float: right;
background: #FFF8BF url(/accidental/wp-content/themes/worn/images/close2.gif) 3px 3px no-repeat;
position: relative;
z-index: 3;
top: -20px;
text-indent: -3000px;
overflow: hidden;
height: 21px;
width: 32px;
}

Note before styling: Since Safari doesn’t allow styles on their form elements, I can’t add the JavaScript fade effect to the submit button like we do in the other browsers. So, what I’ve done is given the indicator image an id of indicator-safari if the browser is Safari, and an id of indicator for all other browsers. This will allow you to give Safari something nice as well as keep the cool submit-button-replace effect in other browsers. swan

  • Atanas Yanev July 6th, 2006 @ 6:45 am (#)

    Im getting error using the .js files… The error is in prototype.js, line 1726.The code there is:
    _observeAndCache: function(element, name, observer, useCapture) {
    if (!this.observers) this.observers = [];
    // this is the error !!!line!!
    if (element.addEventListener) {
    this.observers.push([element, name, observer, useCapture]);
    element.addEventListener(name, observer, useCapture);
    } else if (element.attachEvent) {
    this.observers.push([element, name, observer, useCapture]);
    element.attachEvent(’on’ + name, observer);
    }
    },

    the script is working, but the IE shows that little stupid annoying yellow triangle in the bottom-left corrner ;)
    congrats for the scripts, its absolutely awesome:)

  • AdamD July 6th, 2006 @ 9:34 am (#)

    Looks pretty neat. Good work, homey.

    And I get what you’re saying about live search. And I think you’re right that there has to be a minimum number of characters, but I think after three or so, it’s useful. If I am typing “supercalifragilistic,” for example, I’d love it to give me a few options after the first three letters.

    As for Google suggest, it’s neat to see the most popular search for each letter, isn’t it? :)

  • Elliot Swan July 6th, 2006 @ 12:16 pm (#)

    @Atanas: Got a link for me to see?

    @Adam: One that starts searching after 3 characters or so might be interesting. Perhaps a form that gives you an option to do a live search or just a normal ajax search…

    Hmm…

  • Atanas Yanev July 6th, 2006 @ 12:21 pm (#)

    Elliot Swan, here is the link pga-bg.com/blog/ (hope to put nofollow to this link or to delete this link after checking, its testing, the real domain will be ayanev.com and will be online in about 12hrs, tha language is Bulgarian, dont be afraid, the search field is in top right corrner, under the login button)

  • Elliot Swan July 6th, 2006 @ 12:28 pm (#)

    I added a nofollow to your link for ya.

    I tested it out in FireFox, IE 6, and IE 7, and none of them gave me any JavaScript errors. Perhaps give the site a hard refresh and try it again?

  • AdamD July 6th, 2006 @ 12:31 pm (#)

    @ Elliot: I fear extra options. I think you go with what you want and try to listen to user feedback. There is nothing wrong with a standard-acting search, like you have. The live element takes out a perceived page-load, so that’s cool. And your intuition about users being confused by suggest-search is probably right on the money.

    @ Atanas: The problem is in prototype. I wouldn’t worry about the little yellow box. It comes up on Elliot’s version, too, and doesn’t keep it from working.

  • Atanas Yanev July 6th, 2006 @ 12:37 pm (#)

    Hey man, you are the quickest developer I’ve ever seen ;)
    Thanks for the no follow ;)

    Here are few screenshots:
    first, when I reload (yup man, hard refresh, no cache.. :( ), I’m getting this in FireFox (latest version)
    http://pga-bg.com/tmp/blog1.jpg

    When i click on the error I’m getting this screen:
    http://pga-bg.com/tmp/blog2.jpg

    here is the error in the latest IE6
    http://pga-bg.com/tmp/blog3.jpg

  • Elliot Swan July 6th, 2006 @ 12:38 pm (#)

    The error that Atanas showed is what happens when Prototype tries to look for an element that doesn’t exist. The “errors” on Accidental Procrastination aren’t actually from the search, they are because the .js file is looking for a comment form that doesn’t exist on that page. If you go to a post page, the “errors” won’t show.

    Though actually, they are more of a warning than an error, and shouldn’t show up unless you have a debugger turned on.

    I didn’t see any such warnings on Atanas’s site, though. Did you, Adam? If not, then I’d recommend Atanas just trying a hard refresh to see if they go away. If Adam can see them, then I’m not sure what to say.

    EDIT: hah, we were posting at the same time. I’ll take a look.

  • Atanas Yanev July 6th, 2006 @ 12:42 pm (#)

    Yup, Im shocked :D

    THANKS for taking your time for this :)
    As i wrote in my first post - IT IS WORKING AWESOME, just alerting, I’ll put big credits under the search results in the final version (hope godaddy will run my domain fast ;) )

    I’ll put try { }catch(e) to prevent this :)
    Will write about the effect in about 10minutes :)

  • Elliot Swan July 6th, 2006 @ 12:44 pm (#)

    Oh, I know why now. It looks like you decided not to have the close button in there, so the Event Listener for that close button is looking for an element that doesn’t exist. If you don’t want the close button, then to get rid of the error take out these lines in search.js (17 and 18):

    
     Event.observe('closeresults', 'click', live.Close, false);
    18 $('closeresults').onclick = function() {return false;};
    

    That should do it for you. :)

    EDIT: I realize now that I forgot to include how to use the Close button…Apologies. Article updated. :)

  • Atanas Yanev July 6th, 2006 @ 12:47 pm (#)

    Ahhh.. Elliot, SORRY for bothering you with this false alarm :(
    I tryed to modify the look-and-feel of your code, I’m so stupid :)
    SORRY once again and thanks for the quick solution.
    Your search is AWESOME ! I cant imagine a better one!

  • Elliot Swan July 6th, 2006 @ 12:50 pm (#)

    No, actually, I just realized that my instructions were incomplete–I forgot to include how to use the close button. If you’d like that close button, add this after the results div:

    
    <a id="closeresults" href="#" title="Close search results." style="display: none;">Close</a>
    

    I’ve updated the article to include this. Thanks for letting me know, sorry about that. :)

  • Atanas Yanev July 6th, 2006 @ 1:09 pm (#)

    Have no words. I updated the script, added bulgarian text for “close”… I really dont know how I lost the “close” button, but that is working absolutely super ! ;)

    Thanks !
    I put a link to your website in the results, if you want - I will change this to whatever link/text you like :)

    ps
    ayanev.com is working for me, 1hr after purchasing from godaddy .com and webhosting4life .com :D
    Today is good day, found #1 search script, my website went alive for under a hour… ;)

  • […] Better-Than-Live AJAX WordPress Search: […]

  • Zeo July 6th, 2006 @ 11:03 pm (#)

    Another version of LiveSearch. Neat!

    Btw, you could use the Insertion object for the searchresults and closeresults so it could work out of the box.

    Well there’s a lot more trick you could try with Prototype.

  • […] Iba a hacer un manual de como montar un Buscador en vivo y de paso lo implementaba en el theme, pero este manual es muy bueno y lo explica bastante bien. […]

  • […] El tipo de búsqueda lo denominaron “mejor que viva” y gracias al CSS pude hacer que el searchbox tenga la cruz para cerrar la búsqueda y el spinner que dice que “AJAX está trabajando para usted”. Los invito a buscar lo que sea (cualquier bug es por el mal uso de la cajita). […]

  • Billytheradponi (Nick Barrett) July 16th, 2006 @ 12:11 pm (#)

    Woot! Finally one of this type of searches available to the public, thank you so much! Can I ask, how do you do this ‘live commenting’? Could you tell me @ Random Shapes (on the forum, i’m new)…

  • Orin July 17th, 2006 @ 4:10 am (#)

    Super cool plugin. I’ve gotten it up and running exactly how I want it now and it just… makes me feel special inside. My feeling is that once more Wordpress users discover this technique, it’ll be all over the place. It’s just head and shoulders above “normal” search pages.

    I also wanted to tell ya about a little bug I accidentally discovered, then fixed. Yup. I was having this problem on every page besides the front—the search would start, wait, then disappear and not show anything. Don’t know why it happens, but when I turned on the Nice Search Plugin the problem vanished. You seem to have it running on yer other site too and for whatever reason, it makes the difference.

  • Elliot Swan July 18th, 2006 @ 11:46 am (#)

    @Orin: Actually, I don’t have the Nice Search plugin installed.

  • Billytheradponi (Nick Barrett) July 20th, 2006 @ 8:06 am (#)

    Mine just isnt calling the javascript, when I press ok it just takes me to the search results page (just the one included, so no styling). Im afraid I cant show you because its on my local server before I upload the theme to my site…But nothing happens, but I have done everything you said…

  • Billytheradponi (Nick Barrett) July 20th, 2006 @ 8:08 am (#)

    ahh dont worry, I included the results in header.php not searchform.php, I go confused, sorry :D

  • John July 21st, 2006 @ 6:24 am (#)

    Is there a working example of this somewhere?

  • Elliot Swan July 21st, 2006 @ 6:44 pm (#)

    @John:

    From the beginning of the article:

    I first implemented it over at Accidental Procrastination, so you are welcome to go check it out there.

  • Chris August 18th, 2006 @ 6:44 am (#)

    Elliot…

    Awesome search, but i seem to be having some problems with it on my site http://www.cagintranetworks.com/projects/.

    When i click on OK to search, it just takes me to a page with the unstyled search results. I have done everything possibly that i can think of to fix it.. all my paths are correct, and the search.php file calls a header file i modified to include just the js files, css (but it didnt work with the original wordpress header either for me)

    I am sure you will need more info… but i would appreciate any help!

    Thanks!

  • […] In the near future I wish to add AJAX functionality to display search results (with post titles) under the search text box without a page refresh. I looked into a few ready-made solutions to do this, but after too many failed attempts with the SearchLite and Live Search WordPress plugins and the Better-Than-Live AJAX WordPress Search, I think I may have to create something myself. […]

  • […] First off, let me start off by saying that Elliot did a great job designing this better-than-live-search., I love it now that I finally have it working correctly on this blog but I had a hell of a time getting this to work, but I loved the concept so much that I had to keep plugging away at it until it was fixed. […]

  • Leanne August 26th, 2006 @ 7:02 pm (#)

    I’m able to get the search form and search results to display on one of my websites, but I’m a novice about javascript files - where do they need to go in order to be functional? I put them in with the theme, tried them in the base directory, but am not having any luck. Just need to know where to put effects, prototype and search.js files.

    Thank you!

  • Francesco August 26th, 2006 @ 8:50 pm (#)

    Hi, is not working for me neither. If I hit the “enter” key, it will bring me into an index page with the ?s=searchterm suffix, but it won’t show the effect.

    Here is the link:

    htttp://www.aibu.it/web-usabile/

    Many thanks in advance if you could check it out

  • Chris August 27th, 2006 @ 12:59 pm (#)

    I had the same problems (as you can see from my above post) i got it working and instead of re-typing everything, i posted about it…

    http://www.cagintranetworks.com/projects/2006/08/better-than-live-search-from-elliot-swan/

  • Elliot Swan August 27th, 2006 @ 1:44 pm (#)

    @Leanne: It doesn’t matter what directory you put them in as long as you use the right path when including them–check out Chris’s article.

    @Francesco: You haven’t included any of the javascript files. They need to go in your header, check out Chris’s article as well.

  • Francesco August 30th, 2006 @ 2:36 am (#)

    Hi, again! I have made it wor and I wat to thank you all for the help. But now I am having a new problem. I have changed the index page to be in the root of my server, while the blog is on it’s own directory which is called web-usabile. Now from the option page of wordpress admin I have changed the path to the index page and the blog directory, but when I try to include the wp-blog-header it works only with the index page, and all the other give me an error. Beside this the search is not working anymore because it suffer from the same error as well.

    Is there anyway to include the path to the wp-blog-header.php file in an absolute way?

    P.S I am using permalinks so this may be the problem.

    Should you want to try it you can still find it under
    http://www.aibu.it

    Thanks a lot,
    Francesco

  • Scott September 14th, 2006 @ 5:19 pm (#)

    Now I gotta figure out how to implement this on a web page, searching through a MySQL database. The implementation is the best I’ve seen, but still over my head!

    I want to have it search through a list of stocks (stock market) that a user can search by symbol, and it’ll display the company name. Any direction would be much appreciated.

  • othersite September 15th, 2006 @ 5:19 pm (#)

    Wonderful script Elliot! I was hoping you could help me out.

    The search was working correctly for a little while (actually, in one window), but now its not. The indicator just sits there - no results are displayed.

    http://www.othersite.org

    Please check it out, thanks!

  • Paint the Tiger • Carve the Swan » Ajax Search September 17th, 2006 @ 6:54 am (#)

    […] This is very cool. Better than Live Search. I might be able to implement this, and even include the abililty to get multiple pages worth of results. […]

  • Matt Schinckel September 18th, 2006 @ 6:04 am (#)

    I’ve implemented your idea over at my site, but the code was all mine :)

    Thanks for the concept though.

  • Lucas Everett September 27th, 2006 @ 3:07 pm (#)

    Elliot,

    Thanks for the great script. It is exactly what I’ve been looking for. It is working great, but I noticed something that I’d like to change. When I attempt a new search after having previously performed a search, the search box updates and then goes away and comes back down. I noticed that on your site it doesn’t behave that way. How do I make the box disappear and then come back down after the search has updated? If you visit my blog, you’ll see what I mean.

    Thanks!
    Lucas

  • Elliot Swan September 27th, 2006 @ 3:14 pm (#)

    @Lucas: Actually, it does that on my site as well. In order to fix that, you’ll need to run a conditional checking whether the box is already open or not, and if it is close it before starting the search.

    Perhaps a next version of the script is in order… :)

  • Derrick October 5th, 2006 @ 9:53 pm (#)

    Love the plugin and using it on my site (though a little less pretty than here). Is it posible to get the search link results working with AjaxWp (http://www.giannim.com/blog/index.php?page_id=13)?

    Thanks.

  • Elliot Swan October 6th, 2006 @ 8:46 am (#)

    I took a look at your site and it seemed to be working already?

  • […] ajax — Better than live ajax search […]

  • Derrick October 8th, 2006 @ 5:02 am (#)

    Not quite, but close. The problem is the links being created in the search results are not processed as Ajax links. I’m not sure why. I tried to add an image onLoad to your Better-Than-Live search to call the AjaxWp initialization function, but it’s still not getting picked up. Not sure what the trick is that I’m missing …

  • Thorsten October 9th, 2006 @ 2:46 am (#)

    Hello, great job!!!

    Is there a way for me to use that stuff without using WP?? I want to have that search on one of my sites but I don’t use WP at all (there’s no need for me at the moment).

    What can I do?? Any ideas?

    Regards

  • Derrick October 9th, 2006 @ 8:27 pm (#)

    Thorsten: Point the search form to a different script–whatever one you use to search your website. That should really be the only thing you need to change.

  • Thorsten October 10th, 2006 @ 2:51 am (#)

    Hello Derrick,

    thanks for your quick answer. I played around with that for hours but without success. I’ve got an example page (no design and javascript behind - just a template with a small basic example search). There you can see the page structure how it would be build in the future. I don’t use header and footer at the moment.

    http://www.erfrischend-guenstig.de/ajax_search/index.php

    I think I will have to find someone who can do this for me. I’m not pro enough to get the things working. So, if anyone can help here’s the file with that little php inside:

    http://www.erfrischend-guenstig.de/ajax_search/index.zip

    I like this ajax search so much, but maybe I will have to do it without it.

    All the best,
    Thorsten

  • Derrick October 12th, 2006 @ 1:15 am (#)

    I’m kind of confused. Whats the point of the example page you linked? It doesn’t include the necessary JavaScript from this tutorial and it doesn’t have the required SearchResults <div> in the page (it only appears on the returned page after I click through).

    Is this supposed to be an alternate way of doing this? I’m kind of confused. Regardless of what you target to generate your results, you absolutely have to have that <div> and the JavaScript, otherwise there’s nothing Ajax about this.

  • Thorsten October 12th, 2006 @ 2:22 am (#)

    Write a comment here.

  • Thorsten October 12th, 2006 @ 5:40 am (#)

    Derrick, yes, as I said it’s just a basic template without all necessary “Better-Than-Live Stuff”. It’s just a small example to show where the ajax search should appear. Well I doesn’t understand the tutorial until it’s end and so I don’t know where to place some things and in the correct manner. For that I provided the download in hope someone could get my file working by including all the necessary stuff at the right place and in the correct manner.

    I’m new to ajax and don’t want to use WP (never took a look at WP, cause I don’t use blogs), that’s my problem.

    ————————————

    Okay, let’s start from the beginning (I’ve provided a new file at the bottom):

    1. “First, we of course need to get ourselves a form” —-> I did that !
    2. “We’ll also need a div to show the results in” —-> I did that !
    3. “For that nifty little close button, we need our close link” —-> I did that !
    4. “Now to do the searching. I’ve included a file called search.php”
    —-> Where to inculde that file? I think inside the searchresults div (that would make sence :) - I did that !
    5. (inside search.php): “replace yourpath to header.php” —- I don’t use WP or any header/footer concepts, so I didn’t do that !
    6. “Bring in the JavaScript” —-> All ones: scriptaculous, prototype, effects and search?? I did that !
    7. Open up search.js “Set the variable url in line 72 to the path to your WordPress index” —–> I don’t use WP so what to do here - I linked to search.php!?
    8. “CSS-styling” —-> I didn’t do that at the moment, cause want to see the thing working first !

    Please take a look at that: http://www.erfrischend-guenstig.de/ajax_search/index2.php (I made further steps)

    Do you see the searchresults appear -> disappear -> appear again at the first time the page is loading (by doing the first search)? And why isn’t the movement quite smooth? Another thing: isn’t there a solution sliding up the searchresults div first when doing a new search?

    It would be fine if you could take a look at those latest files if there’s something I’ve forgotten:

    http://www.erfrischend-guenstig.de/ajax_search/index2.zip

    All the best

  • Elliot Swan October 12th, 2006 @ 11:00 am (#)

    Hey Thortsen,

    Everything is right except for step four. There is no need to include search.php in the search results, so take out this from index2.php:

    <?php include('includes/search.php'); ?>

    Then everything should work.

    Nice job. :)

  • Thorsten October 13th, 2006 @ 4:14 am (#)

    Hi Elliot, I did what you said, but that wasn’t the mistake. I’ve forgotten to give “display: none” to the searchresults DIV. I think the thing is working right know: http://www.erfrischend-guenstig.de/ajax_search/index2.php

    But there are two more very important things I would like to know:

    1. If I don’t wanna use the close button how would a “

  • Thorsten October 13th, 2006 @ 4:22 am (#)

    Oooops, it broke my post, so here again the rest:

    But there are two more very important things I would like to know:

    1. If I don’t wanna use the close button how would an a href=… has to be if I wanna close the searchresult from somewhere else within my site (for example a textlink)?

    2. It was a request from someone else before: “… When I attempt a new search after having previously performed a search, the search box updates and then goes away and comes back down…”

    Isn’t there a way to let the searchresults div disappear (slide up) first? That would make your work perfect! Maybe something with onFocus or onSubmit or something like that? I don’t know javascript on its’ best so I can’t do that on my onw :-(

    All the best … I like your work !!

  • Thorsten October 14th, 2006 @ 12:35 am (#)

    Hi Elliot,

    because of the effect I was talking about one post before, it should be something like that (use the textlinks 01, 02 and 03): http://www.erfrischend-guenstig.de/ajax_search/index3.php

    Did you take a look at my last post, because of closing the searchresults from somewhere else?

    All the best

  • Elliot Swan October 14th, 2006 @ 10:45 am (#)

    For the closing before reopening, I already mentioned that above.

    To close somewhere else, just move your link to somewhere else, keeping the ID.

  • Thorsten October 15th, 2006 @ 10:00 am (#)

    Hi Elliot,

    “Perhaps a next version of the script is in order… :)”

    Did you mean that? I hope to see that new version soon, because only with this add on I’m going to build it into my new site :) Without I think the ajax-search looks a bit buggy.

    I’m VERY interested in your next version - GOOD JOB !!

    All the best,
    Thorsten

  • Todd Kozan October 15th, 2006 @ 12:36 pm (#)

    Hi Elliot,

    Amazing search tool! Thank you.

    I’ve been reading all the above threads but for the life of me I cannot get this to work.

    The indicator keeps spinning and no search results are displayed.

    My test blog (clealry a work in progress) is here –> http://www.innapinch.com/blog/

    Any help greatly appreciated!

    Thank you!

  • Joe October 18th, 2006 @ 11:23 pm (#)

    Excellent bit of code! I changed the method to GET so I could wrap a condition around the search form. This way I am able to have both a normal search page and the stripped-down AJAX version.

    Also, if Javascript is not enabled, the full search page displays.

    For the AJAX version, you just have to add ‘&ajax’ parameter to the URL then check for it in the search.php form.


    <?php if (isset($_GET['ajax']) && $_GET['s']) { ?>
    /* do bare results for AJAX form */
    <?php } else { ?>
    /* do full search code with template header */
    <?php } ?>

    At the bottom of the search.js file, change:

    var pars = 's='+$F('search_input');

    To:

    var pars = 's='+$F('search_input')+'&ajax';

    And change:

    var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, onComplete: this.Complete, evalScripts: true});

    To:

    var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onComplete: this.Complete, evalScripts: true});

    The GET code idea came from Steve Smith at Orderedlist.com.

    If you want to see it working: http://www.vibetechnology.com.

    At some point I’d love to add the check Steve does to set the default search input text *only* when JS is available (otherwise you can’t blank it out and user has to delete it first).

    Oh, and a blank search check would be nice… :-)

    Cheers!

  • Joe October 18th, 2006 @ 11:49 pm (#)

    Hmmm. The clear-on-click-but-only-with-Javascript was easy. Just add this line to the bottom of the init function (change the text to whatever you want, but make sure it matches the Clear function):

    $('search_input').value = "[enter search]";

    Then you can remove the value=”[enter search]” from the search form. If JS is turned off, it is blank, if it’s on, the input box shows the text.

  • Elliot Swan October 19th, 2006 @ 9:09 am (#)

    @Joe: Nicely done.

    @Everybody: I’ve started development on the next version, so everybody be watching the feed for an update. It will be way easier to implement, so people shouldn’t have as many problems. :)

  • adrian kirsten October 23rd, 2006 @ 12:12 pm (#)

    Elliot, how you doing buddy?

    I got this problem on my site (which unfortunately is being kept under wraps on my local machine until completion, so no show and tell just yet!) when implementing this rather nice script of yours.

    When I’m searching, it simply goes to a completely unstyled page… which is nothing like the nice sliding down thingy you got going! I want sliding down thingies!

    anyways, tantrums aside, I’m very new to javascript (i’m a pictures person) and I simply cannot figure this out! I saw some other dude had the same problem, but i couldnt find the solution when browsing above commentation. IS there a solution? I noted earlier on, a few days ago it also happened on this site. I did everything right, even the correct order of putting them files down! (didnt know order was important up until now!)

    anyways, shot dude. I don’t know if you even look at these comments anymore, but here’s hoping!

  • Joe October 23rd, 2006 @ 2:39 pm (#)

    Adrian,

    The search page is stripped down so it will look correct in the “sliding down thingy” :-)

    There are three main parts to this:

    1. a stripped-down search.php to get bare minimum of results
    2. an AJAX query to load the results in the background
    3. scriptaculous effects to make the invisible div appear as a slider
    4. (optional) provide a full-featured search result when step #2 fails

    Sounds like step #2 is failing for you because you do not have the entry in the header.php file working. Make sure each step works before proceeding.

    I’m so happy with Elliot’s code here, I’m willing to help a little - send me the three files you have (stripped of anything secret, of course), and I’ll take a peek.

    Hope this helps!

  • Elliot Swan October 23rd, 2006 @ 4:46 pm (#)

    Adrian, make sure that your search.js file is being included after prototype.js and effects.js.

  • Consulenza web e usabilita’ November 6th, 2006 @ 5:16 am (#)

    Sorry, but I don’t understand a think. What does happen if the browser have javascript disabled? Does it show the normal search page or it does give an error?

    Many thanks anyway,
    is a beautifull plugin!

  • Matt Schinckel November 6th, 2006 @ 4:01 pm (#)

    That is a good question. I rolled my own solution, and this was something I built into it.

    The easy solution is to make the form act normally, and then add an onsumbit=”Search(); return false” to it, so it doesn’t go to the new page.

  • Joe November 6th, 2006 @ 4:52 pm (#)

    What does happen if the browser have javascript disabled? Does it show the normal search page or it does give an error?

    The results are the same in either case, but if there’s no JS, the results will be returned in a stand-alone, bare page.

    If you want to have a condition where the results are normal, you can modify the calling code as I did above (it’s not as hard as it looks and I’m happy to send HTML to any who want it).

    I have it running on my site too.

    Excitedly waiting for the new version, though! :-)

    Good luck!

  • Ignacio January 8th, 2007 @ 4:07 am (#)

    Hi there,
    Great script, congratulations and thanks for share.

    The only problem I’ve seen is when you do a second search and the previous results are visible, firts it shows the new results and then the animation takes place. The result is strange.

    I’ll try to fix and post results.

    Thanks

  • XpUiRXxpfu January 12th, 2007 @ 6:50 am (#)

    Hi! Very nice site! Thanks you very much! aWNObbD2o2

  • Coder Keitaro » Ajax search January 24th, 2007 @ 8:12 am (#)

    […] I got the main part of the code at better than live, and used the Silk iconset. Share:These icons link to social bookmarking sites where readers can share and discover new web pages. […]

  • Deetoy February 10th, 2007 @ 7:11 am (#)

    Very nice search plugin or can I call it a plugin? :)
    Anyway, I got it working on Wordpress 2.0x but not in wordpress 2.1 ..

    The sliding functions etc. is working but the search result just says:

    Not Found

    The requested URL /header.php was not found on this server.

    Someone tried this in the new wordpress 2.1 version and got it working?

  • vldptfmuyd February 15th, 2007 @ 11:03 am (#)

    vknneaq

  • Freddie Carthy February 17th, 2007 @ 10:55 pm (#)

    I got the search to work almost perfectly! There’s two things that seem to be not working properly though.

    (1) After performing a search, the search button never comes back, even after “closing” the search.

    (2) I placed a progress.gif image in my folder and linked to it fine, but it won’t show it when performing a search.

    Also, how do I place the close button at the top instead of at the bottom?

  • Raz88 February 18th, 2007 @ 9:18 pm (#)

    Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/.onyx/chantaljoy/chantaljoy.com/blog/wp-content/themes/GreekGamingGoddess/search.php on line 3

    Warning: include(http://www.chantaljoy.com/blog/wp-blog-header.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/.onyx/chantaljoy/chantaljoy.com/blog/wp-content/themes/GreekGamingGoddess/search.php on line 3

    Warning: include() [function.include]: Failed opening ‘http://www.chantaljoy.com/blog/wp-blog-header.php’ for inclusion (include_path=’.:/usr/local/php5/lib/php’) in /home/.onyx/chantaljoy/chantaljoy.com/blog/wp-content/themes/GreekGamingGoddess/search.php on line 3

    I keep getting these errors

    -Raz88

  • Raz88 February 18th, 2007 @ 9:21 pm (#)

    Disregard Last Comment Problem Solved. :D

    -Raz88

  • […] Alright so the blog has moved to the brand new Ubber Server. This new server is shared with a couple of friends, Tom and Dom. From what they tell me we can do a lot more than webhosting, like remote computer access and all that stuff I probably won’t use, but well, I guess it’s pretty cool. In addition I am working on a new theme that you can see live right now. I could quite give up the grey color but I really want to bright it up overall. Also there are a couple of cool things that you won’t see immediately. The first thing is Live search implemented in the sidebar. Basically you won’t need to get to a new page to get your search results. This is an JavaScript that you’ll find over here. The second cool thing is that my theme now can be viewed on a 800×600 screen resolution. I am using another JavaScript that immediately identified the visitor’s resolution and calls different CSS properties whether you are higher or lower than 800×600. You’ll find this script by looking at the source of this page) Below 1024x 768, the sidebar displays at the bottom with few addition to make it look better than a simple fluid theme. […]

  • Tuding February 21st, 2007 @ 12:02 pm (#)

    Hey, Elliot nice search feature!

    I would love to use this but i can’t seem to get it working.

    Am using wordpress 2.1 and when i search the results appear in a new page which is very bare just a list and white page.

    Thanks

  • […] Better-Than-Live AJAX WordPress SearchDie "Better Than Live Search" sieht so aus, als wollte sie die Tage ganz dick bei mir ins Blog gesprungen kommen :-) […]

  • hi-fi lover March 11th, 2007 @ 1:15 pm (#)

    Awesome search, but i seem to be having some problems with it on my site http://www.cagintranetworks.com/projects/.

    I think there is no problem :) Nice work.
    I confirm - awesome search!

  • Nathan March 13th, 2007 @ 9:43 pm (#)

    Thanks for the search Elliott. Just added it to my site last weekend. Love it!

  • Robert Felty March 14th, 2007 @ 2:04 pm (#)

    That is pretty nifty, though I didn’t realize that the search box on your page was a search box at first. The styling made me think that it was like a tagline — Elliot Swan: better than live search. The same goes for the “publish your thought” button, and the name and e-mail textboxes. They just look like normal content to me, not forms.

    Just some food for thought.

  • Kungfuice.com » Ajax Search March 20th, 2007 @ 8:30 am (#)

    […] Well I spent a good portion of yesterday implementing Better Then Live Search and can finally say that I have everything looking and working the way I wanted. […]

  • hubs March 23rd, 2007 @ 4:13 pm (#)

    I’m getting this error and can’t seem to figure out how to correct it:

    Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/.jerrie/hubs/artifacting.com/blog/wp-content/themes/Cleaker2.0/Cleaker/search.php on line 3

    Warning: include(http://www.artifacting.com/blog/wp-blog-header.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/.jerrie/hubs/artifacting.com/blog/wp-content/themes/Cleaker2.0/Cleaker/search.php on line 3

    Warning: include() [function.include]: Failed opening ‘http://www.artifacting.com/blog/wp-blog-header.php’ for inclusion (include_path=’.:/usr/local/php5/lib/php:.:.:.:.’) in /home/.jerrie/hubs/artifacting.com/blog/wp-content/themes/Cleaker2.0/Cleaker/search.php on line 3

    Any help would be greatly appreciated. thanks Elliot.

  • adamw April 1st, 2007 @ 10:24 am (#)

    Thanks for your work with this script ! It’s simply awesome !

    I had many problems to make it work with my WP 2.1, but finally i’ve worked it out.

  • Aen Tan April 13th, 2007 @ 11:06 pm (#)

    Great search feature. I’m using it for a site I’m building.
    http://lushliving.sg/proto/features/main-feature/modern-classy-and-grand/

    I changed some things. Like disabling the submit button in search.js when “Active” and enabling it when “Complete”. I notice when the submit button is pressed 2 times, 2 indicators appear and only 1 goes away.

  • Bar. Watta cool name. BD April 30th, 2007 @ 9:26 am (#)

    thx, i will surely use it on my website.

  • hubs May 10th, 2007 @ 3:29 pm (#)

    I have it working on my blog now. Thanks for this great tool!

  • andres May 13th, 2007 @ 2:30 am (#)

    hi, sir, any possibilitu to put example files with built-in codes?

    i have no idea where to start, where put the codes and all ..

    thank u, can u e mail me back please sir?

    THANK U SO MUCH

    BYE

  • […] Ajax ile WordPress içi arama K12 temasýnda bulunan arama þekli. Sanýrým burada Elliot Swan | Better-Than-Live AJAX WordPress Search anlatýyor, ama yabancý dilde olduðu için kavrayamadým . Yardýmcý olabilirseniz çok mutlu olacaðým.. Teþekkürler. […]

  • vhiifhkubb May 24th, 2007 @ 9:36 am (#)

    sqfgsn

  • antis3ptic May 30th, 2007 @ 1:18 am (#)

    Hi, nice job its working on wp2.2, but i dont want to click enter button, i want to write my search word,after wait and see results. can i make it. look it in here: http://addictedtonew.com/archives/145/wordpress-live-search-plugin/

  • Elliot Swan June 27th, 2007 @ 7:46 pm (#)

    To anybody watching these comments who is interested, this script has been updated to version 2 over here: http://www.elliotswan.com/2007/06/27/better-than-live-ajax-wordpress-ready-search-version-2/

  • dizisaati September 28th, 2007 @ 1:39 pm (#)

    Write a comment here.thx, i will surely use it on my website.

  • Sheep January 9th, 2008 @ 10:25 am (#)

    This could be very cool! Oh wow, I like it a lot!

  • Paco April 23rd, 2008 @ 11:59 am (#)

    Thanks for this good work !

Say Something.





I'm a nice guy, so I'll let you use basic XHTML such as <a>, <strong>, <em>, <blockquote>, and <code>. If you're trying to share some code with us, just make sure to run it through Postable first.

Write your comment