tags:

views:

33

answers:

1

I have an <input> of type text that I want to populate with a value from a database using AJAX. First, I define my text zone like the following:

<td><input type=text id='st'  value=" " name='stname' onclick="donnom();" /></td>

In javascript, I do the following:

xhr5.onreadystatechange = function(){
    if(xhr5.readyState == 4 && xhr5.status == 200) {
        selects5 = xhr5.responseText;
        // On se sert de innerHTML pour rajouter les options a la liste
        document.getElementById('st').innerHTML = selects5;
    }
};
xhr5.open("POST", "ajaxIDentifier5.jsp", true);
xhr5.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
id = document.getElementById(idIdden).value;
xhr5.send("id=" + id);            

In IDentifier5.jsp, I put the next code:

<%
String id = request.getParameter("id");
System.out.println("idDailyTimeSheet ajaxIDentifier5 as is:" + id); 

Session s = null;
Transaction tx; 

try {
    s = HibernateUtil.currentSession();
    tx = s.beginTransaction();
    Query query = s.createQuery(
        "select from Dailytimesheet dailytimesheet " +
        "where dailytimesheet.IdDailyTimeSheet=" + id + " ");         

    for(Iterator it=query.iterate();it.hasNext();) {
        if(it.hasNext()) {
            Dailytimesheet object=(Dailytimesheet)it.next();

            out.print( "<input type=\"text\" id=\"st1\" value=\"" +
                       object.getTimeFrom() +
                       "\"   name=\"starting\" onclick=\"donnom()\" ></input>");
        }
    }
} catch (HibernateException e) {
    e.printStackTrace();
}
%>

I want to get only the value in the input type text populated from database, because after that I will be able to change it.

+2  A: 

Obviously, your query returns only one result (WHERE ID = ...) that you want to put in your textfield.

Strange things:

  • The page you are calling via AJAX is priting HTML code, but your are trying to use it as the innerHTML of a element.
  • It seems you want to add an input more than populating an existing one. Why has it different id/name ?

Maybe instead of :

document.getElementById('st').innerHTML = selects5;

You want to do :

document.getElementById('st').value = selects5;

But then you will need to return only the result of the query in your JSP, not HTML code :

out.print(object.getTimeFrom());
mexique1