Я хотел сделать параметры от search-box
до select
, которые пользователь извлекает из MySQL динамически.
Я попытался retrieve
данные из MySQL и сделал их доступными вsearch-box
.
Функция AJAX
<script>
var request = new XMLHttpRequest();
function searchStudent() {
var s_Name = document.vinform.s_Name.value;
var url = 'Student.jsp?val=' + s_Name;
try {
request.onreadystatechange = function() {
if (request.readyState == 4) {
var val = request.responseText;
document.getElementById('mylocation2').innerHTML = val;
}
}; //end of function
request.open('GET', url, true);
request.send();
} catch (e) {
alert('Unable to connect to server');
}
}
</script>
Страница JSP
<label class="col-sm-3 text-right control-label col-form-label">Search:</label>
<div class="col-sm-3">
<input type="search" name="s_Name" onkeyup="searchStudent()" class="form-control" placeholder="Search Candidate" autofocus="autofocus">
<span id="mylocation2" style="height: 10px; width: 20%;"></span>
</div>
Student.jsp
<%
String s_Name = request.getParameter("val");
if (s_Name == null || s_Name.trim().equals("")) {
out.print("<p style=\"color: red;\">Please enter Student name!</p>");
} else {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/geniustechnology", "root", "");
PreparedStatement ps = con.prepareStatement("SELECT * FROM enquiry_master WHERE enquiry_name LIKE '" + s_Name + "%'");
ResultSet rs = ps.executeQuery();
if (!rs.isBeforeFirst()) {
out.println("<p style=\"color: red;\">No Record Found!</p>");
} else {
out.print("<table frame=\"box\" width=\"1024\" style=\"height: 36px; width: 100%;\">");
while (rs.next()) {
//out.println("<div style=\"width:500px; border-style: solid; border-color: red;\"><table border=\"1\" style=\"margin-left: 180px; margin-top: 5px; width:auto;\">");
out.print("<tr><td>" + rs.getString(2) + "</td></tr>");
}
out.print("</table>");
} // end of else for rs.isBeforeFirst
con.close();
} catch (Exception e) {
out.print(e);
}
%>
Как показано в screenshot
ниже, я могу видеть, что database
пришел из базы данных, но not
может select
его.
![image](https://i.stack.imgur.com/NHV0z.png)