Skip to content
the poor coder

form data object

Poor Man
Published
Reading time
1 min read

Form data objects are used to store and send form data to a server. It can be used to store any type of data in key-value pairs, including strings, numbers, and even files. Form data objects are created using the HTMLFormElement.createFormData() method. This method takes an array of objects and creates a form data object from the key-value pairs of the objects.

Text

            // Create the form data object
            var formData = new FormData();
            
            // Add the data to the object
            formData.append('name', 'Raju');
            formData.append('age', '25');
            formData.append('image', imageFile);
        

Form data objects can also be used to send data to the server using the XMLHttpRequest.send() method. The data is encoded using the multipart/form-data encoding type.

Text

            // Create the request
            var request = new XMLHttpRequest();
            
            // Set the method, url and encoding type
            request.open('POST', '/upload', true);
            request.setRequestHeader('Content-Type', 'multipart/form-data');
            
            // Send the request
            request.send(formData);
        

Form data objects can also be used to read data from the server. The FormData.get() method can be used to read a specific value from the object.

Text

            // Get the name field from the form data object
            var name = formData.get('name');
        

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC