The dropdown in the HTML file gets populated dynamically. Here, I am passing the form value to the servlet via Ajax and the servlet generates the table data which gets appended to the table. The problem I am facing is while sending the parameters via Ajax.
Servlet (DealerSearch.java) -
public class DealerSearch extends connection {
private static final long serialVersionUID = 1L;
String sql,dname,i,table;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
get_connection();
if(request.getParameter("dsel")!=null)
{
dname = request.getParameter("dsel");
}
if(request.getParameter("name")!=null)
{
dname = request.getParameter("dname");
}
System.out.println(dname);
sql = "select * from dealers_info where dealer_name='"+dname+"'";
st = con.prepareStatement(sql);
rs = st.executeQuery();
table="<thead>" +
"<tr>" +
"<td>Dealer ID</td>" +
"<td>Dealer Name</td>" +
"<td>Dealer Address</td>" +
"<td>Region</td>" +
"<td>Area</td>" +
"<td>Contact Person</td>" +
"<td>Contact Number(O)</td>" +
"<td>Contact Number(M)</td>" +
"</tr></thead><tbody>";
while(rs.next())
{ table+="<tr>";
table+="<td>"+rs.getString("Dealer_ID")+"</td>";
table+="<td>"+rs.getString("Dealer_name")+"</td>";
table+="<td>"+rs.getString("Dealer_address")+"</td>";
table+="<td>"+rs.getString("Region_Name")+"</td>";
table+="<td>"+rs.getString("Area_Name")+"</td>";
table+="<td>"+rs.getString("Contact_Person")+"</td>";
table+="<td>"+rs.getString("Contact_No_Office")+"</td>";
table+="<td>"+rs.getString("Contact_No_Mobile")+"</td>";
table+="</tr>";
}
table+="</tbody><table>";
System.out.println(table);
response.getWriter().print(table);
st.close();
rs.close();
close_connection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
HTML -
<form method="post" id="FindDealer" name="FindDealer" class="form-horizontal">
<br/> <br/>
<h2 style="text-align:center;">Dealers</h2>
<p style="text-align:center;font-family:helvetica;font-size:20px;">Select a dealer, or type in a dealer name, for details.</p>
<br/>
<div class="control-group" id="one">
<label class="control-label" for="input01">Dealer Name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="dname" id="input01" onClick="return checkDisabling();" data-provide="typeahead" autocomplete="off" data-source="dealernameods">
</div>
</div>
<p style="text-align:center;font-family:helvetica;font-size:20px;">OR</p>
<div class="control-group">
<label class="control-label" for="select01">Dealer Name</label>
<div class="controls">
<select id="dealerselect" name="dsel">
</select>
</div>
</div>
<div style="text-align:center;font-family:helvetica;font-size:30px;">
<button type="submit" class="btn btn-primary" onSubmit="return onsearch();"><i class="icon-search"></i> Find Dealer</button>
<button type="reset" class="btn"><i class="icon-trash"></i> Cancel</button>
</div>
</form>
<table class="table table-bordered table-striped table-hover" id="dealertable">
</table>
Function -
function onsearch()
{
/* stop form from submitting normally */
e.preventDefault();
alert($('#dealerselect').val());
$.ajax({
type: "post",
url: "/SalesTrackingTool/DealerSearch",
data:{ dsel: $('#dealerselect').val()},
cache:false
}).done(function(data) {
$('#dealertable').empty().append(data);
});
}