Home >Web Front-end >Front-end Q&A >How to enter a value in text in javascript and query the database based on the value
With the development of web applications, JavaScript has become one of the most popular client-side languages. JavaScript can implement a variety of functions, including dynamic web pages, form validation, interactive user interfaces, animation effects, and more. In this article, we will introduce how to use JavaScript to automatically query the database after entering a value in the text box.
Before using JavaScript to query the database, you need to determine the data you want to query. By using database queries you can obtain the required data set. In this example, we will simulate a student management system database that contains information about all students, such as names, grades, and so on. We will use JavaScript to automatically query the database to display the student's grade information when the user enters the student's name in the input box.
In order to query the database, you need to connect to the database. By using AJAX technology, you can send a request to the server and get a response without refreshing the page. In this example, we will use the XMLHttpRequest object to implement the AJAX request. The following is an example of establishing a database connection:
var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // 处理响应数据 } }; xmlhttp.open("GET", "getstudentinfo.php?q=" + str, true); xmlhttp.send();
In the above code, we create a new AJAX request using the XMLHttpRequest object. When readyState changes, we call a callback function to process the response data. Among them, the readyState attribute represents the status of the AJAX request, and the status attribute represents the response status code. By calling the open() method, we can specify the requested URL, as well as the request type ("GET" or "POST"). After calling the send() method, the AJAX request will be sent to the server.
When the user enters characters in the input box, we need to query the database in real time to obtain the corresponding results. In order to listen to input box events, we can use the addEventListener() method to associate an event handler with the input box. The following is an example of listening to input box events:
document.getElementById("input").addEventListener("keyup", function() { var input_value = document.getElementById("input").value; // 查询数据库 });
In the above code, we registered a "keyup" event handler through the addEventListener() method. When the user enters characters in the input box, the The event will be triggered. We get the input box element through the document.getElementById() method, and then use the value attribute to get the value of the input box.
When the user enters characters in the input box, we need to send an AJAX request to the server to obtain the corresponding data. Here is an example of querying the database and displaying the results:
document.getElementById("input").addEventListener("keyup", function() { var input_value = document.getElementById("input").value; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("result").innerHTML = this.responseText; } }; xmlhttp.open("GET", "getstudentinfo.php?q=" + input_value, true); xmlhttp.send(); });
In the above code, we send an AJAX request with the input values to the server and the server will return an HTML fragment containing the student information. When readyState changes, we store the response data in the result element, which will be used to display the student information.
In addition to automatically querying the database when entering a value in the input box, we can also implement the automatic completion function. When the user enters characters in the input box, we can display a drop-down menu with available options. Here is an example of implementing autocomplete:
document.getElementById("input").addEventListener("keyup", function() { var input_value = document.getElementById("input").value; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var options = JSON.parse(this.responseText); var ul = document.getElementById("auto-complete"); ul.innerHTML = ""; for (var i = 0; i < options.length; i++) { var li = document.createElement("li"); var a = document.createElement("a"); a.appendChild(document.createTextNode(options[i])); a.setAttribute("href", "#"); li.appendChild(a); ul.appendChild(li); } if (options.length > 0) { ul.style.display = "block"; } else { ul.style.display = "none"; } } }; xmlhttp.open("GET", "getstudentnames.php?q=" + input_value, true); xmlhttp.send(); });
In the above code, we send an AJAX request with the input values to the server and the server will return a JSON array with the available options. We use the JSON.parse() method to convert the response data into a JavaScript object. We then create an unordered list with options and add it to the auto-complete element. If the number of options is greater than 0, a drop-down menu is displayed.
This article introduces how to use JavaScript to automatically query the database after entering a value in the text box. By using AJAX technology and event listeners, we can achieve a powerful and flexible way of interacting with user interfaces. If you'd like to learn more about JavaScript and AJAX technology, check out the literature and tutorials.
The above is the detailed content of How to enter a value in text in javascript and query the database based on the value. For more information, please follow other related articles on the PHP Chinese website!