Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search data from dynamically created table

I have created one application in that there is one text box for searching information from table. Although i have written the code when we enter the character in search text box, after accepting one character control goes out of textbox. this is my code for searching`

 <script type="text/javascript">
$(document).ready(function()
{
    var minlength = 1;

    $("#searchTerm").keyup(function () {
      
        value = $(this).val();
		
		if (value.length > minlength ) 
		{
			searchTable(value);
		}
		else if(value.length < minlength)
		{
			searchTable("");
		}	
	});
});
function searchTable(value)
{
	$.ajax({
		type: "GET",
		url: "dispatient.php",
		data:({search_keyword: value}),
		success: function(success)
		{
			window.location.href = "dispatient.php?search_keyword="+value;	
			$("#searchTerm").focus();
		},
		error: function() 
		{ 
			alert("Error occured.please try again");	
		},
		complete: function(complete)
		{
			$("#searchTerm").focus();
		},
		
	});
}
<input id="searchTerm" Type="text" class="search_box"  placeholder="Search" 
			   value = <?php echo $_GET['search_keyword'] ?> >

` Please suggest to me..

thanks in advance..

like image 518
Dnyaneshwar Chavan Avatar asked Oct 20 '22 16:10

Dnyaneshwar Chavan


2 Answers

value is default attribute of javascript try to change the variable name of value into something like searchData

like image 100
Narendrasingh Sisodia Avatar answered Oct 22 '22 17:10

Narendrasingh Sisodia


In your success callback, you are redirecting the page to dispatient.php. I believe, this is the same page that has the search functionality. Once you redirect, the page is reloaded again and there is no point in writing:

$("#searchTerm").focus();

Since, you are already using AJAX, try loading the data from success on to your page through JavaScript/jQuery without reloading the page.

like image 37
littleibex Avatar answered Oct 22 '22 17:10

littleibex