Created
April 23, 2021 06:10
-
-
Save dreamer01/707fc3a8cf8c4327ddc4cd113ba7aa22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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