My journey into web programming has led me to learning JavaScript, Node.js, and Express.js. My ultimate goal is to execute a server-side JavaScript function (specifically a function that searches for something in a MySQL database) when a button is pressed on an HTML page.
Here is the directory structure I'm working with: There is a main directory that contains a subdirectory named "page". This "page" folder includes server.js, dbFunctions.js, and a "public" subdirectory. The "public" subdirectory houses index.html, style.css, and various images.
In my server.js file:
var express = require('express');
var app = express();
var path = require('path');
var port = 8888;
// Allow usage of static files
app.use(express.static("public"));
// Start the server
app.listen(port);
console.log("Server running on port" + port);
HTML Example:
<html>
<head>
<title>Test html</title>
</head>
<label for="key">Valid key:
<input id="key" name="key" required>
</label>
<input type="checkbox" name="test" value="testval" checked>
<button type="button">Button</button>
</html>
The HTML content consists of an input field, a checkbox, and a button. Once the button is clicked, it should trigger a function from dbFunctions.js with parameters obtained from the input field and the status of the checkbox as boolean values on the server-side. I've heard about using AJAX calls for this, but the explanations I found were quite confusing. Is there a simpler, more straightforward "hello world" example available?
Thanks in advance!