Last active
November 15, 2022 11:20
-
-
Save attilabacso/ec02f4765e58b6a0b24962fc285c61ec to your computer and use it in GitHub Desktop.
This file contains 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
// SELECT ON CHANGE VALUES FETCHING TO PHP | |
document.querySelectorAll(`select.collect`).forEach(dropdown => | |
dropdown.addEventListener(`change`, (e) => { | |
// GET VALUES | |
let value = e.target.value; | |
let text = dropdown.options[dropdown.selectedIndex].text; | |
let price = dropdown.options[dropdown.selectedIndex].getAttribute(`attr-price`); | |
let name = e.target.name; | |
let data = { | |
name: name, | |
value: value, | |
text: text, | |
price: price, | |
}; | |
// FETCH | |
fetch('response-ajax.php', { | |
method : 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(data) | |
}) | |
.then(response => response.text()) | |
.then(text => { | |
// TRY RESPONSE TYPE | |
try { | |
// JSON | |
let data = JSON.parse(text); | |
console.log(`JSON:`); | |
console.log(data) | |
} catch(err) { | |
// NOT JSON | |
console.log(`NOT JSON:`); | |
console.log(text) | |
} | |
}) | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment