Pass information from an HTML input field to the backend using JavaScript

Currently, I am utilizing Bootstrap within my HTML to develop a search page that will allow users to input a SQL query. Once the user enters their SQL query, it will be sent to a backend system that will execute the query in a database. The backend code is kept separate from the front-end code, which has made it challenging for me to figure out how to transfer the query text to the backend.

I am struggling with coming up with the right kind of function to handle this task. My idea is to create an onclick=function that would be responsible for sending the text to the backend. However, despite searching, I have not been able to find any packages or functions that specifically cater to this functionality. While AJAX appeared to be a potential solution, I did not find anything that fit my requirements precisely.

Answer №1

You can achieve this without the use of any external libraries

// Accessing the search bar element
const input = document.querySelector('input')

// Accessing the button element
const button = document.querySelector('button')

// Adding an event listener to the button
button.addEventListener('click', () => {
  // Initiating an AJAX request
  fetch('http://yoururlhere?query=' + input.value)
})
<input>
<button>Search</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Bootstrap 4.3 navbar dropdown feature and hamburger icon functionality

I tried copying the navbar code from the bootstrap website, but unfortunately the dropdown list and hamburger menu are not functioning as expected. I attempted to include popper.js and meta tags in the code based on some suggestions from Stack Overflow a ...

Guide to Embedding StencilJS components in a Storybook React Application

I am currently in the process of integrating Stencil and Storybook within the same project. While following this setup guide and this one, I encountered a step that requires publishing the component library to NPM, which is not my desired approach. In my ...

Choose JSON information and modify it utilizing NODE.js with identical data

Feeling stuck.. I have a JSON file with some data and I need to manipulate it. Take a look at my JSON structure: [{ "method": "GET", "path": "/", "aliases": "", "name": "rootPath", "handler": "generatedApps/avion01/actions.HomeHandler" }, { "method": "GET ...

Incorrect script enqueue order in child theme of WordPress

Help needed with enqueuing a script in a WordPress child theme. Despite specifying Jquery dependency, the script is loaded before Jquery. The code in question: add_action( 'wp_enqueue_scripts', 'child_theme_scripts' ); function child_ ...

Retrieve a text and save it into a variable

Is there a way to extract a specific string and save it as a variable? For example, if I have the following URL: http://sub.site.com/WordsHere-t.jpg I am looking to extract just WordsHere. The length of this string can vary and will not always be 9 chara ...

Issues with ASP.NET arise when losing the selection in a dropdownlist that was populated using javascript

On my webpage, I have two ASP.NET dropdownlist controls. The first dropdownlist retrieves an array from the server and uses it to populate the second dropdownlist through javascript. However, when I select an option in the second dropdownlist and then perf ...

Creating various functions for Joomla's AJAX component development

My component is currently working smoothly with AJAX and mootools. The view.raw.php file contains only one function called display. I've been attempting to add other functions within the component that can be accessed through AJAX, but have been facin ...

Update the table that includes a php script

I have a piece of PHP code embedded within a table tag that displays text from a database. I am looking for a way to automatically refresh this table every minute with updated content from the database, without refreshing the entire page. While I have co ...

Combining platform-express and platform-fastify for optimal performance

I am currently working on a NestJS application and my goal is to upload files using the package @types/multer. However, I encountered an issue while following the guidelines from the official documentation: https://i.sstatic.net/JCX1B.png Upon starting ...

Attempting to showcase information on the Angular frontend

When attempting to retrieve the Street name, I am seeing [object Object]. What is the optimal approach for displaying JSON data on the client side? I managed to display a street name but struggled with other components. How can I access the other elements ...

Quirky rendering problem in AngularJS

My issue involves creating blocks using data from a JSON file, which includes a background-image path for a div. Here is an example snippet: [ .. { .. .. "smallimg": "../assets/images/dummy/dummy-intro-1.jpg", .. ] On my page, I hav ...

The directives applied within ng-include are not functioning correctly

Below is a custom directive I have implemented. var fancySelectDirective = pluginDirecitvesModule.directive("custom-select",function(){ return { restrict: 'C', link: function (scope, element, attrs) { ...

In order to dismiss the popup message in the component.ts file within Angular 4, I must figure

Currently in my component.html, I have a popup message with opening and closing functionality implemented in html. However, I now need the close functionality to be moved to a method. <ng-template #content let-c="close" let-d="dismiss"> < ...

consistent character spacing for a custom font upload

Developing an application that necessitates counting up and opting for the font Orbitron due to its square appearance. The issue arises as, unlike the default chrome font, the width of this font's digits is not consistent, causing the count character ...

What is the best way to remove double quotes surrounding "name" and "count" when displayed in the JavaScript console output?

I am struggling to transform the input: ["apple", "banana", "carrot", "durian", "eggplant", "apple", "carrot"] into the desired output: [{ name: "Apple", count: 2 }, { name: ...

Tips for improving the speed of loading infinite scroll pages

I am currently working on scraping over 100k rows from the provided URL. The data goes back approximately a month, loading in batches of 7-8 rows at a time. My current approach involves using a macro to scroll down the page slowly, which is effective but ...

Unable to detect backspace events in an Ionic/Cordova application

In the process of developing my Ionic/Cordova app, I encountered a challenge with number input on Samsung devices. It seems that they do not display the decimal separator when the input type is set to number. If this were a native app, I could easily use t ...

What is the process for invoking an External Javascript Firestore function within a Typescript file?

Trying to figure out how to integrate a Firestore trigger written in an external JavaScript file (notifyNewMessage.js) into my TypeScript file (index.ts) using Node.js for Cloud functions. Both files are located in the same directory: https://i.stack.imgu ...

Distinguishing between client side rendering and server side rendering is the backbone of web development

My goal is to give users the option to request webpages from my website either directly from the server or by clicking on links, which will be managed by Backbone's router. When a user requests a webpage directly from the server, they will receive a ...

How to integrate a While loop within an RXJS subscription

I have a piece of Angular code where I am attempting to subscribe to my first API and include a while loop within this subscription. Additionally, I need to subscribe to another API inside the while loop. The reason for this is that I need to subscribe t ...