Search data Using ajax without press search button??

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2008
Posts: 296
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Search data Using ajax without press search button??

 
0
  #1
Nov 21st, 2008
Hi,
I want to search data using Ajax php without pressing on search button.

How to do that??

any script or link demo or sample code ???

i want do this search like orkut search means design ??

plz help me??
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,083
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Search data Using ajax without press search button??

 
1
  #2
Nov 21st, 2008
Originally Posted by Aamit View Post
Hi,
I want to search data using Ajax php without pressing on search button.

How to do that??

any script or link demo or sample code ???

i want do this search like orkut search means design ??

plz help me??
You'll need to know a bit of JavaScript.

Basically, on each type of a few characters into the search input, you send the search to the server, and load the results.

eg: HTML

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input id="search_input" />
  2. <div id="results"></div>

eg: JS

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script>
  2. window.onload = function() {
  3. var inputbox = document.getElementById('search_input');
  4. inputbox.onkeyup = function(e) {
  5. e = e || window.event;
  6. keycode = e.keyCode || e.which;
  7. if (keycode == 13 || this.value.length%3 == 0) {
  8. doSearch(this.value);
  9. return;
  10. }
  11. }
  12. };
  13.  
  14. </script>

Explanation:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input id="search_input" />

is our search inputbox. It is a DOM element, which allows JavaScript to attach event listeners to it.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. window.onload = function() {
  2. // ...
  3. };

In the javascript we first attach an event listener to the window Object, to listen for the onload event. The onload event fires when the browser completely loads the page.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. window.onload = function() {
  2. var inputbox = document.getElementById('search_input');
  3. inputbox.onkeyup = function(e) {
  4. // ...
  5. }
  6. };

Then in the window onload, we attach an onkeyup event listener to the search inputbox. We do this after the window has loaded to make sure the inputbox has been loaded.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. inputbox.onkeyup = function(e) {
  2. e = e || window.event;
  3. keycode = e.keyCode || e.which;
  4. if (keycode == 13 || this.value.length%3 == 0) {
  5. doSearch(this.value);
  6. return;
  7. }
  8. }

Then we add the code for the onkeyup event listener.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. e = e || window.event;

e refers to the Event that we are observing. The Event should be passed to our event listener function as the first parameter, but if it isn't, we can can retrieve it from the global Event, window.event.


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. keycode = e.keyCode || e.which;

Each key on the keyboard has a corresponding keycode, so we retrieve the keycode from the event. It can either be e.keyCode or e.which so we cater for that by using e.which if e.keyCode is undefined.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if (keycode == 13 || this.value.length%3 == 0) {
  2. doSearch(this.value);
  3. return;
  4. }

If the keycode is 13 (ENTER was pressed) or the length of the search text is a multiple of 3, then we do the search.

The multiple of 3 part is what triggers the search each time you type in 3, 6, 9... etc. chars. It's up to you how many should trigger the search.

The doSearch function should to an XMLHttpRequest to the PHP script that does the search, and add the results to the screen dynamically.

XMLHttpRequest can be a bit complicated so it is best to use a XMLHttpRequest Library or Wrapper that will abstract the actual browser dependent code for you and keep your code simple.

My example is based on: http://code.google.com/p/xhr/

So you doSearch could look like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function doSearch(txt) {
  2. new fiji.xhr('post', 'search.php', function() {
  3. if (this.readyState == 4 && this.status == 200) {
  4. document.getElementById('results').innerHTML = this.responseText;
  5. }
  6. }).send({q: txt});
  7. }

What this does is create a new XMLHttpRequest to the page search.php passing it the query as {q: txt} which is just an object, with the property q which is equal to txt. txt is your search.

The server would receive a POST request with the parameters:

q=txt
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if (this.readyState == 4 && this.status == 200) {

this refers to the XMLHttpRequest object instance. the readyState property will be equal to 4 when the request has completed. The status property reflects the HTTP response status, 200 means everything went ok.

document.getElementById('results').innerHTML = this.responseText;

When the XMLHttpRequest completed, we just take the response from the server and insert it into the <div id="results"> Element.

The PHP side is just a normal search page. It should return just the search results, without other HTML for a page.

eg: search.php

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $search = $_POST['q'];
  4.  
  5. $query = "select * from articles where title like '%".mysql_real_escape_string($search)."%' LIMIT 1, 10";
  6.  
  7. $results = $mysql->query($query); // pseudo query
  8.  
  9. foreach($results as $result)
  10. {
  11. echo '<div><a href="#">'.$result->title.'</a></div>';
  12. }
  13.  
  14.  
  15. ?>

This just does a search of the database table articles for a title similar to the search.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 1800 | Replies: 1
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC