I have a created sqlite server that holds JSON data about users. The data looks like this:
{
0:{
id: 1,
name: "Sam Smith",
},
1:{
id: 2,
name: "Jane Smith",
}
}
I also have a basic html page with a form:
<div id="input" class="searchuser">
<form>
<legend class="useradd-fonts">Search Existing User</legend>
<label for="search_byname">Name: <input type="text" id="search_byname"/></label>
<input type="button" id="get-btn" value="Search" /><br>
</form>
</div>
I also have code for getting this data from the server with an XMLhttprequest(), however, the code only allows to get the whole JSON data and display all the users:
const getBtn = document.getElementById("get-btn");
const getData = function() {
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://127.0.0.1:3000/users");
xhr.onload = function() {
const data = JSON.parse(xhr.response);
console.log(data);
};
xhr.send();
};
getBtn.addEventListener("click", getData);
My question is, how can I search a user by their name, so in the HTML form, write Sam Smith, and after clicking the button this will return only the first user's information and not all of the users in the dataset? I would like to do this with XMLhttprequests without using any external libraries, is this possible to do?