troubleshooting Question

Ajax php search with multiple text fields generated by php

Avatar of Crazy Horse
Crazy HorseFlag for South Africa asked on
PHPAJAX
38 Comments4 Solutions1423 ViewsLast Modified:
I have some php code that generates text fields based on a simple calculation. The text field(s) are outputted like this:

echo "<div class='form-group'>";
echo "<input id='search' type='text' autocomplete='off'>";
echo "<div style='display:none' id=loading><img src='assets/img/ajax-loader.gif'/></div>";
echo "<ul id='result'></ul>";
echo "</div>";

I want the user to be able to start typing in any text field and it should perform a database search and start displaying records via ajax.

The problem I have at this moment before I even get onto the actual search code is that the loader only shows up in the first text field. If I starting typing in any text field after the first one, whether it be second or fourth etc, the loader does not appear for the other text fields.

The jQuery/AJAX is as follows:

      
$(document).ready(function(){
			 $('#search').focus();
			 $('#search').keyup(function(){
			 $("#loading").show();
			 	var search = $('#search').val();
				if(search.length >3) {
					$.ajax({
				
					type: 'POST',
					url: 'functions/search.php',
					data: {search:search},
					success: function(data) {
				
					
						if(!data.error) {
							$('#result').html(data);
						 		$("#loading").hide();
					}
				}
			});
				
		}
				if (search.length < 1) {
            		$('#result').html('');
					  $("#loading").hide();
				
			}
		});
	});
SOLUTION
Join our community to see this answer!
Unlock 4 Answers and 38 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 38 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros