如何在jsp中选择<option>时从数据库中获取多个值并显示在文本框中

奇卢卡·巴拉特
  1. 我的表是:用户,它包含这些列。一世)。登录名 ii) 姓名 iii)。名字 iv). 姓氏 v)。地址

在下面,我们在下拉列表中填充名称,一旦下拉值更改,我们需要在文本框中打印相应的名字、姓氏、地址、登录名。这是我们需要实现的

这是我的jsp代码。

        <%   DBConnection dbc=new DBConnection();   
             Connection con=dbc.getNewConnection();
             Statement st = null;
             ResultSet rs = null;
        try
        {
           st=con.createStatement() ;
           rs=st.executeQuery("select * from Users"); 
           %>
 
   <select id="selectBox" >

         <%  while(rs.next()){ %>
                <option><%= rs.getString(2)%></option>


      <%} %>
    </select>
    <input id="loginid" type="text" value="" />
    <input id="firstname" type="text" value="" />
    <input id="lastname" type="text" value="" />
    <input id="address" type="text" value="" />

那么如何根据从用户表中选择的名称填充以上 4 个值
请帮助我如何编写 Javascript 函数来做到这一点。

斯瓦蒂

您首先需要编写onchange事件处理程序,以便在选择框值获取更改时,this处理程序将被调用,然后将值发送select到后端页面(servlet).so,ajax代码将如下所示:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  $('select#selectBox').on('change', function() {
     var value = $(this).val(); //get value from select-box
      $.ajax({
        type: 'POST',//can be post request or get 
        url: url,//put url here where you need to send
        data: {
          'value': value//pass value 
        },
        success: function(response) {
          //result will come here 
           //if recieve as html use           
          $("somedivclass").html(response)        
          //if recieve as separted commas         
          var result = response.split(",")
          //access same using result[0],result[1] ..etc
          //add value to input using
          $("#loginid").val(result[0]);
         //same for other  
    
        }
      });
    })

在您的服务器端,如果发出请求,使用 ajax 的request.getParameter("value")内部doPost方法获取从 ajax 传递的值。然后POST,只需编写您的查询以从 db 检索记录并将其发送回 ajax。因此,您的代码将如下所示:

String value = (String) request.getParameter("value");
String query = "select * from Users where yourcolumnanemtocompare=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, value);
ResultSet rs = ps.executeQuery();
//if value there 
String text;
if (rs.next()) {
  //change value accordingly..i.e : rs.getstring..
  text = "<input id=" + loginid " type="
  text " value=" + rs.getInt(1) + " /><input id="
  firstname " type="
  text " value=" + rs.getInt(2) + " /> <input id="
  lastname " type="
  text " value=" + rs.getInt(3) + " /><input id="
  address " type="
  text " value=" + rs.getInt(4) + " />";
}

response.setContentType("text/html"); // Set content type 
response.setCharacterEncoding("UTF-8");
response.getWriter().write(text); // response to send back.
//or
if (rs.next()) {
  //change value accordingly..i.e : rs.getstring..
  text = rs.getInt(1) + "," + rs.getInt(2) + "," + rs.getInt(3) + "," + rs.getInt(4);
}
response.setContentType("text/plain"); // Set content type 
response.setCharacterEncoding("UTF-8");
response.getWriter().write(text); // response  to send back..
//or use json to send data..

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在VB.NET的文本框中显示数据库中的空值

如何从单个文本框中获取多个值并将其存储在数组中以及如何将其存储在数据库中

如何在Selenium中选择多个元素(不是<Select>-<Option>)

当文本框包含浮点值时如何在MFC中获取文本框的值

如何在数据库中包含引号的文本框中设置值?

如何基于所选选项从数据库MySQL在文本框中显示数据?

用户从下拉列表中选择值时如何在文本字段中显示数据库值

如何从数据库获取数据并将其放在文本框中-vb.net

如何从数据库中选择并填充在Vb上的文本框中。网

如何动态显示使用Laravel从数据库获取的文本框值

如何在jsp中的数据库中对下拉值制作多个依赖的文本框

如何获取多个选择框的值并在文本框中显示结果?

当我选择路径名称时,如何从文本框中的组合框中获取选定的数据库名称

附加<option>并从数据库中选择值

如何在MySQL数据库中插入带有文件上传器值的多个文本框?

如何根据选定的值并参考php中的数据库将值获取到文本框?

如何获取逗号分隔的循环文本框值,在jSON中插入到jquery的mysql数据库中

如何在php的文本框中显示数据库中的数据

在php中选择下拉菜单时,在多个文本框中获取数据库值

如何根据数据库中的下拉选择更改文本框值

如果选择 Combobox vb.net mysql,则文本框中的数据库值

如何根据 ComboBox Selected Change Event 从数据库中获取数据到文本框

如果数量文本框值大于数据库中的可用库存,如何显示消息框

如何将多个动态文本框值保存到 SQL Server 数据库中

如何将 SQL 数据库中的数据显示到文本框

当我从数据库中流出某些值时,如何从 VB 中选择列表框中的多个值?

如何显示从数据库中选择的值并显示在 php 的多个选择框中

如何在codeigniter的选择框中显示从数据库中选择的值

如何根据从 php 组合框中选择的值在数据库中的文本框中显示该值?