Solved

Auto complete search does not get selected

Posted on 2014-02-26
4
370 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 110

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 110

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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extracting content from meta tag PHP MYHTML DOM 3 35
issue with DB import 1 36
Date comparison to same 'wednesday' of previous year 3 28
restriction of entering a a page 5 31
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

740 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