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.
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:
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 ..
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