0
我使用JSP作爲我的HTML的後端,並且當我開始輸入客戶聯繫電話號碼時,我想要獲取客戶名稱。如何根據使用JSP的文本框輸入動態地從數據庫中獲取數據?
HTML:
<html>
<body>
<script>
function evaluation() {
var myBox1 = document.getElementById('milk').value;
var result = document.getElementById('result');
var myResult = myBox1 * 35;
result.value = myResult;
}
</script>
<form action="BillValid.jsp" method="post">
<table width="70%" cellspacing="30" color="#FFFFFF">
<tr>
<th>ENTER THE DETAILS</th>
</tr>
<tr>
<td><font color="#800000">Customercontact</font></td>
<td><input name="cus_contact" type="text" id="contact"></td>
</tr>
<tr>
<td><fontcolor="#800000">CustomerName</font></td>
<td><input type="text" name="cus_name"></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td><font color="#800000">DayConsumption</font></td>
<td><input id="milk" type="text" name="days_con"
oninput="evaluation()"></td>
</tr>
<tr>
<td><font color="#800000">SumInRs</font></td>
<td><input id="result" name="total"</td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
</table>
<input type="submit" name="Register"><input type="reset"
name="reset">
</form>
</body>
</html>
JSP:
try{
String DBuser="root";
String DBpassword="qwerty";
String Connection="jdbc:mysql://localhost:3306/online";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn=DriverManager.getConnection(Connection,DBuser, DBpassword);
out.println("Database Succesfully connected");
String CustomerName=request.getParameter("cus_name");
String contact=request.getParameter("cus_contact");
long customerCon=Long.parseLong(contact);
String dayCons=request.getParameter("days_con");
int Consumtion=Integer.parseInt(dayCons);
String total =request.getParameter("total");
int totalConsume=Integer.parseInt(total);
String sql = "select (CustomerName) from customer where CustomerContact='"+ customerCon+"'";
java.sql.PreparedStatement st=conn.prepareStatement(sql);
java.sql.PreparedStatement pst=conn.prepareStatement("insert into invoice (CustomerContact ,CustomerName ,LitresConsumed ,TotalSum) values (?,?,?,?)");
pst.setLong(1, customerCon);
pst.setString(2, CustomerName);
pst.setInt(3, Consumtion);
pst.setInt(4, totalConsume);
int i=pst.executeUpdate();
if(i>0){
response.sendRedirect("BillData.jsp");
}else{
response.sendRedirect("addcustomer.jsp");
}
}catch(Exception e){
out.println(e);
e.getMessage();
}
我是新來這個JSP平臺,也對堆棧溢出,我花了45分鐘後這一個問題,如果有人能夠縮進代碼,努力將得到高度讚賞。 –
你必須使用'Ajax'。輸入聯繫電話號碼後,您應該調用一個函數,並從數據庫中提取數據並將其添加到文本框中。 –
我將非常感謝全給你,如果你能送我該函數的代碼,我沒有對AJAX的想法。 –