Skip to content

Instantly share code, notes, and snippets.

@dreamer01
Created April 23, 2021 06:10
Show Gist options
  • Save dreamer01/707fc3a8cf8c4327ddc4cd113ba7aa22 to your computer and use it in GitHub Desktop.
Save dreamer01/707fc3a8cf8c4327ddc4cd113ba7aa22 to your computer and use it in GitHub Desktop.
window.addEventListener( "load", function () {
function sendData() {
const XHR = new XMLHttpRequest();
// Bind the FormData object and the form element
const FD = new FormData( form );
// Define what happens on successful data submission
XHR.addEventListener( "load", function(event) {
alert( event.target.responseText );
} );
// Define what happens in case of error
XHR.addEventListener( "error", function( event ) {
alert( 'Oops! Something went wrong.' );
} );
let url="";
if(condition1) url = "xyz";
else url = "abc";
// Set up our request
XHR.open( "POST", url );
// The data sent is what the user provided in the form
XHR.send( FD );
}
// Access the form element...
const form = document.getElementById( "myForm" );
// ...and take over its submit event.
form.addEventListener( "submit", function ( event ) {
event.preventDefault();
sendData();
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment