chauhanRohit Posted February 25, 2014 Share Posted February 25, 2014 Hi, guys this is my first post here thanks for reading it, i was working on a search bar , the search works fine and i would like to add a few more functionalities to it. 1.make the search result as dropdown list from the search bar. 2.make the results from the dropdown list links. can any one know how this can be achieved. this is the code. <?php mysql_connect("localhost","root","") or die("could not connect"); mysql_select_db("test") or die("could not find database"); $output = ''; //collect if (isset($_POST['searchVal']) && trim($_POST['searchVal'])!='') { $searchq = $_POST['searchVal']; $searchq = preg_replace("#[^0-9a-z]#i","",$searchq); $query = mysql_query("SELECT * FROM authors WHERE fname LIKE '%$searchq%' OR lname LIKE '%$searchq%'") or die("could not search"); $count = mysql_num_rows($query); if($count == 0){ $output = 'there was no search result!'; }else{ while($row = mysql_fetch_array($query)){ $firstname = $row['fname']; $lastname = $row['lname']; $output .= '<div> '.$firstname.' '.$lastname.' </div>'; } } } echo($output); ?> at present the results are displayed normally on the page. plz help Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/ Share on other sites More sharing options...
jairathnem Posted February 25, 2014 Share Posted February 25, 2014 Her's how I would acheive it: make the search result output JSON, use JQuery to perform POST request on a defined confition (for ex: entered string length >3), parse JSON and display in dropdown. you can make use of autocomplete() jquery function. Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/#findComment-1470580 Share on other sites More sharing options...
chauhanRohit Posted February 27, 2014 Author Share Posted February 27, 2014 sorry i did not mention this , but i am new to php and can you please help me with the code. plz. Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/#findComment-1470868 Share on other sites More sharing options...
jairathnem Posted February 27, 2014 Share Posted February 27, 2014 sorry i did not mention this , but i am new to php and can you please help me with the code. plz. what have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/#findComment-1470878 Share on other sites More sharing options...
chauhanRohit Posted February 27, 2014 Author Share Posted February 27, 2014 (edited) well i made a search bar that displays the results from the database. and the code is the function that searches the database. please see the code , where should i add the <tr> , <td> tags so that each output is displayed in different rows and each one is a link. now the results are displayed as a single element within div. Edited February 27, 2014 by chauhanRohit Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/#findComment-1470914 Share on other sites More sharing options...
Solution jairathnem Posted February 28, 2014 Solution Share Posted February 28, 2014 write a php to output JSON. Example : <?php require 'connection.php'; $term = $_GET['term']; $sql = "SELECT * FROM `table_name` WHERE `element` like '$term%'"; $sth = mysql_query($sql) or die(mysql_error()); $rows = array(); while($r = mysql_fetch_assoc($sth)) { $rows = $r; } print json_encode($rows); ?> and in Autocomplete call this php and parse the returned value. NOTE: the php isnt secure. It is just an example. Donot deploy the same code. Quote Link to comment https://forums.phpfreaks.com/topic/286511-how-to-make-drop-down-list-in-a-search-bar/#findComment-1471020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.