Priyanka Pawar
Priyanka Pawar

Reputation: 205

fetch value from database on selection of <option> in jsp

I am developing one web based application where I have one list and one dependent textbox. I have added customer names in list. I want customer id to be filled in textbox when I will select customer name in list. I have tried using javascript function but that only helps me to know customer name.

Customer Id is not value of option tag its customer code which I have entered while registration of customer. So I want to fetch customer code from mysql database.

Can any one please suggest me something? This is my jsp code.

        <%   DBConnection dbc=new DBConnection();   
             Connection con=dbc.getNewConnection();

             Statement st = null;
             ResultSet rs = null;

        try
        {
           st=con.createStatement() ;
           rs=st.executeQuery("select cname from CustomerMaster"); 
           %>
<td> 
   <select id="selectBox" >

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


      <%  } %>

Upvotes: 3

Views: 20677

Answers (5)

gfgfdg
gfgfdg

Reputation: 1

$(document).ajaxStop(function () {
    var importModel = window.localStorage.getItem('ImportModel');
    localStorage.removeItem('ImportModel');

    if (!importModel) {
        return;
    };

    importModel = JSON.parse(importModel);


    valueDateTextBox.val(importModel.valueDate);
    bookDateTextBox.val(importModel.bookDate);
    referenceNumberInputText.val(importModel.referenceNumber);
    costCenterSelect.val(importModel.costCenter.toString());

    $.each(importModel.table, function (i, v) {
        addRow(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
    });
});

Upvotes: 0

Mwangi Thiga
Mwangi Thiga

Reputation: 1369

Try something like this:

<td class="cell9"><span style="color:#000000;">
<select id="mySelect" name="county"  onchange="myChangeFunction()">
<%
//Counties fetcher
Map<Integer, County> fetchedCounties=CountyFetcher.fetchCounties();

for(County county:fetchedCounties.values()){

String cName=county.getName();  

int id=county.getId();  

%>

<option value="<%=cName%>"><%=cName%></option>

<%}%>

</select></span></td>

Upvotes: 1

Parkash Kumar
Parkash Kumar

Reputation: 4730

Priyanka Pawar, you need to fetch customer-id in statement query as following:

<%
    DBConnection dbc = new DBConnection();
    Connection con = dbc.getNewConnection();

    Statement st = null;
    ResultSet rs = null;

    try{
        st = con.createStatement();
        /* You need to get customerId here, suppose your cid is customerId from table */
        rs = st.executeQuery("select cid, cname from CustomerMaster"); 
%>

<td>
    <!-- Changing dropdown value will call javascript method populateCustomerId() -->
    <select id="selectBox" onchange="populateCustomerId();">
        <%while(rs.next()){ %>
            <!-- rs.getString(1) would be your customerId set as option value -->
            <option value="<%=rs.getString(1) %>"><%=rs.getString(2) %></option>
        <%} %>
    </select>
    <input id="customerId" type="text" value="" />
</td>

Javascript:

function populateCustomerId(){
    var selectBox = document.getElementById('selectBox');

    /* selected value of dropdown */
    var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('customerId').value = selectedCustomerId; 
}

Upvotes: 3

bydsky
bydsky

Reputation: 1794

You can put the id in the option value property, then use js to fill the textbox.

<option value="id">name</option>

Upvotes: 0

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

What if 2 customers have the same names?

 <select id="yourSelectId" name="nameSelect" >
  <option value="customerId">values</option>
   ......

get the selected value using javascript/jquery by accessing the id of the select

var customerId=document.getElementById("yourSelectId");

Upvotes: 2

Related Questions