javascript get formData code example
Example 1: formdata js
var form = $('form')[0];
var formData = new FormData(form);
or specify exact data for FormData();
var formData = new FormData();
formData.append('section', 'general');
formData.append('action', 'previewImg');
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,
processData: false,
});
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.
Example 2: 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>
Example 3: js get data from form
const form = document.querySelector('form');
const data = new FormData(form);
Example 4: js retrieve form data
var nameValue = document.getElementById("uniqueID").value;