search in real time

Discussion in 'PHP' started by Alice24, Mar 12, 2011.

  1. #1
    i want to make something like google when some one search something it gives you the most searched words in real time before to hit the enter tab. what i want is to make in the field on search to show words from a specific row in database in real time ... this is javascript i guess or ajax... need help guys. :D
     
    Alice24, Mar 12, 2011 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    Bit of JavaScript and PHP, this should get you started.. let's say we're searching for movie titles in a database.

    Your HTML input code would be this:
    <input type="text" name="title" id="title" />
    Code (markup):
    JavaScript (edit: this is for JQuery):
    
    $(document).ready(function() {
    	// Detect a change in the div#title field
    	$('#title').change(function() {
    		// Store title in a var
    		var title = $(this).val();
    		
    		// Make an ajax request to search for titles
    		$.getJSON('ajax.php', { title: title }, function(json) {
    			// json of results returned, check json object is not empty and loop through returned results
    		});
    	});
    });
    
    PHP:
    PHP:
    
    <?php
    // Check if title is set, maybe implement a check on length before querying database also, strlen($var) > 3 for example
    $title = isset($_GET['title']) ? mysql_real_escape_string($_GET['title']) : '';
    // Title isnt empty so lets query database
    if(!empty($title))
    {
    	// Select titles where movie title is like wildcard search
    	$query = mysql_query("SELECT `title` FROM `movies` WHERE `title` LIKE '%{$title}%'");
    	// Check results are in database
    	if(mysql_num_rows($query) > 0)
    	{
    		// Loop through returned results
    		while($row = mysql_fetch_assoc($query))
    		{
    			// Store results in an array
    			$array[] = $row;	
    		}
    		
    		// Output the array as json
    		echo json_encode($array);
    	}
    }
    
    PHP:
     
    crazyryan, Mar 12, 2011 IP
  3. Alice24

    Alice24 Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    the script is ok but i have no ideea why is not working. maybe i need a jquery library or...
     
    Alice24, Mar 12, 2011 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    You need to include the jquery library on your page yes, it's not going to work without you modifying it to fit with your code. 90% of the code is there for you to use with comments, read the manuals on php.net and jquery.com ..
     
    crazyryan, Mar 12, 2011 IP
  5. Alice24

    Alice24 Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    I have tried with this
    <script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js” type=”text/javascript”></script>
    PHP:
    but again no result...i didn't find anything on php.net and i looked up on jquery.com and read about JSON... this should be received from a php file in an array (wich in our case is
    the mysql_query put on the echo with json_encode($array); ) but i don't think the problem is in the php file in our case, it must be on the JSON function ... any help will be much appreciated
     
    Alice24, Mar 13, 2011 IP