Solved

Auto complete search does not get selected

Posted on 2014-02-26
4
358 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
  • 3
4 Comments
 
LVL 108

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 108

Accepted Solution

by:
Ray Paseur earned 500 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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now