Jump to content

how to make drop down list in a search bar


chauhanRohit
Go to solution Solved by jairathnem,

Recommended Posts

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

 

Link to comment
Share on other sites

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 by chauhanRohit
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.