views:

86

answers:

2

I am using Ajax to do a live search and all i want is the data t be displayed in a div called "results". However when im ding the search, it displays the search form again and then the results div. How do I go by solving this? Here is the ajax code:

function finding(str)
{
    if (str.length==0)
      { 
          document.getElementById("results").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("results").innerHTML=xmlhttp.responseText;
            }
  }

xmlhttp.open("GET","toy_search.jsp?query="+str,true);
xmlhttp.send();
}

I have added the search page here:

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*, toyShop.*,java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"&gt;

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Toy Search</title>
    <script type="text/javascript" src="js/search.js"></script>
</head>
<body>



  <h2>Toy Search</h2>
    Find toy: <input type="text" name="query" id="query" onkeyup="finding(this.value)" /><br>

    <%
        ToyManager toyM = new ToyManagerImp();
        ArrayList<ToyData> toy_data = new ArrayList<ToyData>();
        String toyName = null;
        String toyBrand = null;
        String desc = null;
        if(!MySQLConn.getInstance().isConnected()){
            MySQLConn.getInstance().connect();
        }
        if(request.getParameter("query") != null && !request.getParameter("query").equals("")){
            String query = request.getParameter("query");     
            toy_data = toyM.searchToy(MySQLConn.getInstance(), query);
        }
    %>
    <div id="results">
    <%
        if(toy_data != null && !toy_data.isEmpty()){
        for(int i = 0; i < toy_data.size(); i++){%>

                   <%=toy_data.get(i).getToyName()%>
                   <%= "<br>"%>

    <%    }
        }
    %>
    </div>

</body>
</html>

A: 

Without seeing the rest of your code, it's hard to tell. If you want to remove the search field from the page and just display the results, I suggest somewhere in the following function you add a line of javascript that updates the css style on your search div to display: hidden.

xmlhttp.onreadystatechange=function()
  {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("results").innerHTML=xmlhttp.responseText;
            }
  }
i have added the search page now.
Deepesh
So you want to perform a search on each key press, and then once the user is done searching, the search field should disappear?
yes but the search field will still appear, only the results should appear below the search field.
Deepesh
A: 

You are asking for a complete web page with your Ajax call, and you're getting one back. It's working as designed, as they say. The problem is not with your Ajax, but with what you are returning from the server. You should be returning search results data, not a web page that happens to have search results data on it. Make the display of the static HTML elements on the page conditional on the method used to get the page. For instance, if the Ajax call looked like this:

xmlhttp.open("GET","toy_search.jsp?query="+str+"&format=fragment",true);

you would hide the HTML because of the &format=fragment part of the URL, leaving you with only the HTML that should be injected into the results div. In normal (non-Ajax) use, where the page is either being opened initially or there is a direct link to search results, the &format=fragment portion of the URL would not be present, and the user would load the entire page.

Stan Rogers
Hi i have tried this but still it returns a whole web page. Any other solution?
Deepesh
Could you show us how you're hiding the extraneous HTML?
Stan Rogers
just the way yu suggested above or is it something else?
Deepesh
You need to show code if you want help. No code, no way to help you.
Stan Rogers