Jump to content

Livesearch


smus

Recommended Posts

Hello, everyone! I'm trying to create a livesearch, but the code doesn't work:

[ls.php]

<html><head>	<title>php and ajax search</title>	<script type="text/javascript" src="../jquery.js"></script></head><body><script>function showHint(str) {	$("#textBoxId").change(function() //triggers when you change the value in your textbox    {       var value = $(this).val(); //gets the value of your textbox       $.post("search.php", {id:value},function(data)           $("#results").append(data);        });     }}</script><p><b>Start typing a name in the input field below:</b></p><form>First name: <input type="text" onkeyup="showHint(this.value)" id="txt"></form><p>Suggestions: <span id="txtHint"></span></p></body></html>

[search.php]

<?   include("../dbconfig.php");   $s=($_POST['s']);   $sql=mysql_query("SELECT * FROM companies WHERE name LIKE '" .$s. "%' ") or die(mysql_error());   while($row=mysql_fetch_array($sql))     {	   echo $row['name']."<br>";	 } ?>  
Link to comment
Share on other sites

what about it doesn't work? what do you want it to do that it isn't doing? Are you checking for or getting any errors?

Link to comment
Share on other sites

You'll have to provide more detail that. Where is it failing? I asked if you were checking for or getting any errors. How are you validating each step of your code works? What logging and debugging have you tried? Try and trace the code through each line and make sure you actually know what each line is doing and confirm that with logging.

 

A couple things I see:

1. I wouldn't use a keyup event on the element in line and an event handler. I would just use the event handler

2. You are POSTing an object, and in your PHP are you looking for it as 's'. That doesn't line up at all with what you're sending

Link to comment
Share on other sites

You're mixing jQuery and regular Javascript in a weird way. Your element has a keyup handler where you run showHint, and inside the showHint function you attach a change handler to the element. So every time you press a key it's going to add a new change handler to the element. By the 5th keypress it's going to try and fire off 4 or 5 ajax requests. If you're going to attach your handler with jQuery then do it once, after the page has loaded.Your jQuery is also trying to target the ID textBoxId, but you don't have an element with that ID.

Link to comment
Share on other sites

You'll have to provide more detail that. Where is it failing? I asked if you were checking for or getting any errors. How are you validating each step of your code works? What logging and debugging have you tried? Try and trace the code through each line and make sure you actually know what each line is doing and confirm that with logging.

 

A couple things I see:

1. I wouldn't use a keyup event on the element in line and an event handler. I would just use the event handler

2. You are POSTing an object, and in your PHP are you looking for it as 's'. That doesn't line up at all with what you're sending

In general, the script is not showing the results from database after I'm typing the values.

2nd point: yes, should I use the name of the 'id' field? this way?

 

$s=($_POST['txt']);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...