java - how to get value from ajax response in different textfields if there is more than one value in response? -


i create program in response coming ajax have 2 values testcode , testname, have 3 text fields in jsp page , 1 testid, when run program , enter testid value in first text field , press tab ajax response coming in testcode , testname present. problem in 2nd , 3rd text field both values(testname,testcode) coming together, want particular testname go particular field ,not both values in same field.

code is:

index1.jsp(jsp file)

 <%@ page language="java" contenttype="text/html; charset=iso-8859-1"         pageencoding="iso-8859-1"%>     <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">     <html>     <head>     <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">     <title>insert title here</title>     <script type="text/javascript" language="javascript" src="a.js">     </script>     </head>     <body>     <form  method="post" action="hi">     test_id:<input type="text" name=testid onblur="mango1(this.value)">     test code:<input type="text" name="testcode" id="tc">     testname:<input type="text" name="testname" id="tn">      <br><input type="submit" value="submit">     </form>     <div id="k"> no test name!</div>     </body>     </html> 

a.js(java script)

function mango1(testid) {     alert("this mango1 "+testid);      var xmlhttp;      if(window.xmlhttprequest)         {          xmlhttp=new xmlhttprequest();          }     else         {          xmlhttp=new activexobject("microsoft.xmlhttp");         }       xmlhttp.onreadystatechange=function()     {          if(xmlhttp.readystate==4 && xmlhttp.status==200)             {             /*document.getelementbyid("k").innerhtml=xmlhttp.responsetext;*/             var item=xmlhttp.responsetext;             document.getelementbyid("tc").value=xmlhttp.responsetext;             document.getelementbyid("tn").value=xmlhttp.responsetext;              }             };             /*xmlhttp.open("post","hello?testid="+testid, true);*/             /*xmlhttp.open("post","hello?testid=", true);*/             xmlhttp.open("post","hi?testid="+testid, true);             xmlhttp.send();      } 

hi.java (servlet getting response database)

package one;  import java.io.ioexception; import java.io.printwriter; import java.util.vector;  import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import dao.testdao;  /**  * servlet implementation class hello  */ @webservlet("/hi") public class hi extends httpservlet {     private static final long serialversionuid = 1l;      /**      * @see httpservlet#httpservlet()      */     public hi() {         super();         // todo auto-generated constructor stub     }      /**      * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)      */     protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         // todo auto-generated method stub         dopost(request,response);     }      /**      * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)      */     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         // todo auto-generated method stub         response.setcontenttype("text/html");         printwriter out=response.getwriter();         string testid=request.getparameter("testid");         system.out.println("testid "+testid);         /*if(testid.equals("1"))         {             out.write("hiv");         }         else         {             out.write("urinetest");         }*/         testdao t1=new testdao();         vector vec=t1.getname(testid);      out.write((string) vec.get(0));      out.write((string) vec.get(1));     }  } 

testdao.java (java file database action)

package dao;  import java.sql.*; import java.util.vector;    //import beans.test;  import dao.dbcon;  public class testdao {         public vector getname(string testid)     {         connection con=dbcon.getconnection();          system.out.println("got connection");          vector temp=new vector();          try         {             resultset rset=null;             preparedstatement pst = con.preparestatement("select testcode,testname test test_id=?");             system.out.println("got control here");             pst.setstring(1,testid);              rset=pst.executequery();                  while(rset.next()){                       temp.add(rset.getstring(1));                       temp.add(rset.getstring(2));                      system.out.println("got control here in loop");                  }                 }         catch(exception e)         {             e.printstacktrace();         }             return  temp;                }      } 

use separator between

out.write((string) vec.get(0) + ",");  out.write((string) vec.get(1)); 

when response use split split text , set both fields like

var responsetextvar = xmlhttp.responsetext; var item=responsetextvar.split(','); document.getelementbyid("tc").value=item[0]; document.getelementbyid("tn").value=item[1]; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -