开发者

ajax to populate an input type text

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 + " ");         

    fo开发者_C百科r(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.


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜