3

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>


      <%  } %>
2
  • would you like a php solution? Commented Dec 19, 2014 at 7:54
  • My project in jsp/servlet Bolboa Commented Dec 19, 2014 at 8:00

5 Answers 5

3

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; 
}
Sign up to request clarification or add additional context in comments.

7 Comments

@Priyanka Pawar: This should satisfy your requirement.
Superb its working. Thank you so much Prakash Kumar.
Hey in mysql database table customer name values are customer codes instead of name its saves code.
What are these code, may be its related data stored in some other table.
Actually customer code is foreign key in my sales table. And i want to store customer code and customer name both in my sales table. when my servlet get request it stores customer code in place of customer name
|
2

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

2 Comments

yes using javascript I am getting name of customer but how to retrive that customer's id from mysql
select cname from CustomerMaster to select id, cname from CustomerMaster ..I hope id is the column name..the rs.getInt(1) in option's value
1

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>

Comments

0

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

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

3 Comments

I am talking about customer id which in my mysql database we can say customer code which I have entered while storing customer information
Then you need change you sql: rs=st.executeQuery("select id, cname from CustomerMaster")
Please take a look at my edited question. And textbox will be filled after selection on list. I am providing only customer names to list.
0
$(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]);
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.