Skip to content

Instantly share code, notes, and snippets.

@vporoshok
Created May 27, 2017 12:42
Show Gist options
  • Select an option

  • Save vporoshok/7589869495ab27029b5fc7e927c2e6ab to your computer and use it in GitHub Desktop.

Select an option

Save vporoshok/7589869495ab27029b5fc7e927c2e6ab to your computer and use it in GitHub Desktop.
var container = document.getElementById('widget');
var result = document.createElement('div');
var form = document.createElement('form');
form.innerHTML = '<input name="message"><br><input name="receiver"><br><button>Send</button>';
form.addEventListener('submit', e => {
e.preventDefault();
var data = new FormData(form);
var req = new XMLHttpRequest();
req.open('post', 'http://localhost:4000/form.php');
req.addEventListener('loadend', e => {
result.innerHTML = req.statusText;
if (req.status === 200) {
form.reset();
}
});
req.send();
});
container.appendChild(result);
container.appendChild(form);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment