Converting HTML input data to JSON using Client-Side JavaScript

I have a form in my HTML that collects user input, and I want to convert the form data into JSON using client-side Javascript.

Check out the code update below~

If the params information is accurate, how can I use JavaScript to post to the API?

UPDATE:

Here is the solution that worked for me:

<div class="uk-width-1-1">
                <p class="uk-button uk-button-default uk-margin" id="demo" onclick="myFunction()">Submit</p>
                </div>

                <script>
                function myFunction() {
                    // Data extraction
                    console.log("Retrieving information");

                    var d = new Date(); // Current date
                    var n = d.getTime(); // Milliseconds since epoch
                    var dateTime = String(n); // Converting to string          
                    var name = document.getElementById("logForm-name").value; // Form field data
                    var employee = document.getElementById("logForm-employee").value; // Form field data
                    var comments = document.getElementById("logForm-comments").value; // Form field data

                    var params = 
                        {
                          "dateID":   dateTime,
                          "visitorName": name,
                          "employeeName": employee,
                          "comments":  comments
                        };
                    JSON.stringify(params); // Converting to string

                    // Sending data
                    var xmlhttp = new XMLHttpRequest();   // Creating new HttpRequest instance 
                    var theUrl = "API URL";
                    xmlhttp.open("POST", theUrl);
                    xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
                    console.log("Sending...");
                    xmlhttp.send(JSON.stringify(params));
                    document.getElementById("demo").style.color = "green"; // Visual feedback for success
                    console.log("Item added to Database.");

                    // Resetting form
                    document.getElementById('logForm').reset()
                }
                </script>

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

"Enhance Your Online Shopping Experience with Woocommerce Ajax Filters and

I have a structure that looks like this: Company Google Apple Microsoft Material Wood Metal Plastic Essentially, Brand & Material are attributes in the Woocommerce system, while the rest are variables. I am currently working on implementing AJAX fi ...

JQuery unable to retrieve the value from a radio button

Currently, I'm facing an issue with a form featuring image-based radio buttons. The problem arises when attempting to initiate an AJAX call based on the chosen value (either 10, 30, or 100). Despite selecting a value other than 10, it keeps defaulting ...

The contextual type 'AnyObject' is not compatible with dictionary literal due to inconsistent data structure

I am attempting to create a basic dictionary in Swift: var dict = [ "_id": "123", "profile": [ "name": "me", "username": nil, ] ] as [String : Any] However, I am encountering an issue with Contextual type 'AnyObject' cannot be use ...

The information in the Vue data is not showing up on the webpage

While developing my application with vue.js, I encountered an issue where the data was not being output as expected. Strangely enough, when I logged the data to the console using console.log(), everything appeared correctly without any errors in the consol ...

Unique identifiers and peculiar symbols for the process of serializing data using flat buffers

I have a Json dataset similar to this: { "!type": "alarm", "$": { "12279": { "!type": "alarm", "title": "Default", "$": { "5955": { "!type": "alarm", "name": "Wake", "day": "SUN", " ...

Issue encountered: The Android build is failing with the error message stating that "null is not

I recently integrated a private gitlab repo (react native module) into my App's package.json. The index file within my private gitlab repo can be viewed here: https://i.sstatic.net/uObHe.png For iOS methods, please refer to this link: https://i.ssta ...

AJAX Patch requests in Laravel

My modal is designed to update information related to various countries. // Here is a snippet of the modal HTML code <div id="EditModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div c ...

How to Retrieve the Value of <input type=date> Using TypeScript

I am currently developing a survey website and need assistance with retrieving user input for two specific dates: the start date and end date. I aim to make the survey accessible only between these dates, disabling the "take survey" button after the end da ...

Is it possible to install a Chrome extension specifically for YouTube on Google Chrome?

Hey, I'm trying to eliminate thumbnail images from YouTube. The code I am currently using is: while (true) { $("ytd-thumbnail").remove() } As of now, when I input this code in the console, it successfully removes all thumbnail images. However, I ...

How can JQuery's .load() method be best utilized when working with an image?

I am currently utilizing the .load() function to fetch an image from the server once a button on my webpage is clicked. I then wrap the image in "a href" and "img" tags to convert it into a clickable link, and finally attach it to a div. Here is the code ...

What is the method for bringing in Mongoose to utilize json.parse()?

I'm really eager to utilize this code for importing a JSON file into JavaScript var treeData; var oReq = new XMLHttpRequest(); oReq.onload = reqListener; oReq.open("get", "test.json", true); oReq.send(); function reqListener(e) { treeData = JSON. ...

Is there a way to make the console output more visually appealing with some styling?

What techniques do programs such as npm and firebase use to generate visually appealing and informative console output during command execution? Consider the following examples: $ firebase deploy or $ npm i <some-package> ...

Toggling the legend and row on click functionality in Google charts

I have a specific requirement that involves a Google chart: Once a Google chart is loaded, I need the ability to click on a legend, which will then gray out the legend and remove its corresponding value from the graph. If I click on the grayed-out le ...

What is the best way to merge two JSON arrays using Axios in a React project?

I am currently working on getting data using axios React from Laravel, but I am facing some challenges in combining two arrays by their respective Ids. In my case, I am trying to retrieve product data and images from the Laravel Controller. Can anyone prov ...

Exclusive pair of vertices within a network

I am working with a diagram that includes nodes A, B, C and several edges connecting these nodes. How can I extract the distinct pairs (A, B), (A, C), (B, C)? One potential method is: visited = []; for item1 in nodes: for item2 in nodes: if (item ...

Create a graph that can retrieve and showcase information stored in a Rails database

Is there a way to incorporate a graph into a Ruby on Rails application that can access and show data from a database file (.rb)? If so, what would be the most optimal approach for achieving this? ...

Add slides in PowerPoint before or after the currently selected slide to enhance your presentation flow

I'm currently working on a function that pulls data from an API to convert PowerPoint presentations into base64 strings. Once I receive the response object, my goal is to seamlessly insert the slides into the existing presentation, exactly after the s ...

Utilizing JavaScript to create a single selection from two Listboxes

Seeking assistance with integrating ASP.NET (C#) code. In my web application, I have implemented two listboxes - one displaying a list of companies fetched from the database (lstCompanies), and the other allows users to filter these companies (lstFilter). ...

The onPlayerReady function in the YouTube API seems to be experiencing a delay and

After following a tutorial on integrating the YouTube API into my website, I encountered difficulties trying to play a YouTube video in fullscreen mode when a button is pressed. Despite following the provided code closely, I am unable to get it to work as ...

Using an array inside a for loop to follow the same structure

I am in need of prompt assistance on how to integrate my array into a for loop. Currently, my graph accepts the following as nodes: var classes = [ {"name":"test.cluster1.item1"}, {"name":"test.cluster1.item2"}, {"name":"test.cluster1.item3"} ...