document.getElementById('myForm').addEventListener('submit', function(event) { event.preventDefault(); const formData = new FormData(this); const jsonObject = {}; formData.forEach(function(value, key) { jsonObject[key] = value; }); fetch('your-php-script.php', { method: 'POST', body: JSON.stringify(jsonObject), headers: { 'Content-Type': 'application/json' } }) .then(function(response) { return response.json(); }) .then(function(data) { console.log(data); }) .catch(function(error) { console.error('Error:', error); }); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter