I have a question about javascript.

I have to use the post function in the javascript codes located in the html file header to send data in the queryString variable to the autocom.jsp file located in the local server and then get data back from my local database using the callback function, function(data) { }.

My problem is that when I input c in the input field of the html page, testing ok is displayed on the webpage, even when in my database I do not have a data in the last name column (called last in the database) which start by letter c. This should never have occurred since data.length >0 is not true in this particular case.

Could somebody please help me with this issue?


The html file is search.html below:



<link href="styles.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" src="jquery-1.4.2.js"></script>

<script type="text/javascript">
 function checkSubmit (thisForm) {
  if(thisForm.last.value=='') {
     alert('Please Enter Last Name');
     return false;

<script type="text/javascript">
function lookup(inputString) {
           if(inputString.length == 0) {
              out.println("testing go");
           } else {
                $.post("autocom.jsp", {queryString: "" +inputString+ ""}, function(data){
                    if(data.length >0) {
                       $('#autoSug').html('<h1>testing ok</h1>');



<form method="post" action="search.jsp" onsubmit="return checkSubmit (this);">
Enter Last Name: <input type="text" name="last" value="" id="suggestions" id="autoSuggestionsList" onkeyup="lookup(this.value);" /><br>
<input type="submit" value="Submit">

<p id="autoSug">  </p>

<form method="post" action="home.html"> 
<input type="submit" value="Back To Home">


Below is the autocom.jsp file:

<%@ page language="java" import="java.sql.*" %>

<% response.setContentType("text/html");%>


String str=request.getParameter("queryString");

String connectionURL = "jdbc:mysql://localhost/jsptest";
Connection con;
con = DriverManager.getConnection(connectionURL, "root", "Dummy123");  // Get a Connection to the database
String sql = "SELECT last FROM employees WHERE last LIKE ' " +str+  " % ' LIMIT 10 "; //Add the data into the database
Statement stm = con.createStatement();
ResultSet rs= stm.getResultSet();
 while (rs.next ()){
     out.println(  "<i onclick='fill("  +  rs.getString("last")  +   ");'>" +rs.getString("last")+ " </i>" );