Answers for "js get formdata"

6

formdata js

var form = $('form')[0]; // You need to use standard javascript object here
var formData = new FormData(form);

or specify exact data for FormData();

var formData = new FormData();
formData.append('section', 'general');
formData.append('action', 'previewImg');
// Attach file
formData.append('image', $('input[type=file]')[0].files[0]); 

Sending form

Ajax request with jquery will looks like this:

$.ajax({
    url: 'Your url here',
    data: formData,
    type: 'POST',
    contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
    processData: false, // NEEDED, DON'T OMIT THIS
    // ... Other options like success and etc
});
After this it will send ajax request like you submit regular form
with enctype="multipart/form-data"

Update: This request cannot work without type:"POST" in options since all
files must be sent via POST request.
Posted by: Guest on July-02-2020
7

javascript get form data

<html>
    <form id="myForm">
        <input type="text" name="email" value="[email protected]">
    </form>
    <p id='text'></p>

    <script>
        window.setInterval(()=>{
            var myForm = document.getElementById('myForm');
            var text = document.getElementById('text');

            text.innerText = myForm.elements['email'].value;
        }, 1);
    </script>
</html>
Posted by: Guest on June-07-2020
0

js get form data

document.addEventListener('submit', function (event) {

	event.preventDefault();

	fetch('https://xxx.xxx', {
		method: 'POST',
		body: new FormData(event.target),
	}).then(function (response) {
		if (response.ok) {
			return response.json();
		}
		return Promise.reject(response);
	}).then(function (data) {
		console.log(data);
	})
});
Posted by: Guest on March-26-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language