?
Solved

Auto complete search does not get selected

Posted on 2014-02-26
4
Medium Priority
?
377 Views
Last Modified: 2014-03-17
Hello,

I want to implement auto search completion in php. I am attaching file for this. But i can not select the value and put into text box.

Any help will be deeply appreciated.

Thanks.
text-box-search.php
gethint.php
0
Comment
Question by:ManiBhushan Kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39888741
Please use the code snippet to post the code samples.  It gives us a unispace font and line numbers that facilitate conversation.

text-box-search.php:
<script>
function showHint(str)
{
var cat= <?php echo $doctorId ?>;

var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str+"&p="+cat,true);

xmlhttp.send();
}
</script>
 <div class = "search_container" style="width:400px;">
       
        <input type="text" name="SelMed" id="key" class="search" onChange="add_dosage(this.value, '')"  onkeyup="showHint(this.value)" />
        <div id="txtHint"></div>

Open in new window

gethint.php
<?php
include("include/dbconnect.php");


 $sql = "select *from tbl_medicines WHERE doc_id LIKE '".$_REQUEST['p']."' AND medicine_name LIKE '".$_REQUEST['q'].'%'."' ";
$res = mysql_query($sql);
     
       
while($row = mysql_fetch_array($res))
{
	?>
	<div class="show" style='cursor:pointer;padding-bottom:5px;'>
	
	 <span class="name" style=" border: solid 1px rgb(10, 6, 6); background-color: aquamarine; "> <?php echo ucwords($row['medicine_name']);?></span>

	</div>
<?php
}
?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39888760
From a quick first look at these two code snippets, it appears that you might want to get a foundation in how PHP and JavaScript work.  To that end, this article will point you to some good learning resources and more importantly, help you avoid the many terrible examples of code that litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The first example should be discarded and replaced with jQuery.  Make a Google search for "jQuery Autocomplete" and you will find many good examples (but make sure you understand the basics before you choose something randomly from an online search).  EE has an article that can help you get started with jQuery and AJAX.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

The second example uses the MySQL extension, and that will have to change.  PHP is doing away with MySQL support soon (it's already deprecated) so you will want to choose MySQLi or PDO.  In my experience, the conversion is easier if you choose the object-oriented version of the MySQLi extension.  This article will help you get through the necessary conversion.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

A map of the familiar but obsolete MySQL functions and their corresponding MySQLi and PDO equivalents is available on my web site here:
http://www.iconoun.com/mysql_mysqli_pdo_function_map.php
0
 

Author Comment

by:ManiBhushan Kumar
ID: 39897894
can anyone help me ? please
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39898514
We may be able to help you, but surely you understand that this is not a question with an answer, it's more like "I have an apple; help me bake an apple pie."  There is some learning, there are some other ingredients, and it takes some time.  That is why I pointed you to the learning resources that will help you get enough of a knowledge foundation to begin a project like this.  You cannot expect to be successful if your approach to software development is copying some pieces of found code without understanding what you've copied.  But if you deconstruct the problem, you can make great progress and we can help guide you along the way.

The first thing you want to do is make sure your "background script" works correctly.  You should be able to test gethint.php from the browser address bar - just type the URL, adding in your parameters for p and q.  Let's try to get that working.  When you have it working, or when you find that you're stuck on the task, please post the URL here so we can show you the basics you will need to secure the script and avoid external attacks that can steal your data base.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question