Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

FORM api

const btn = document.querySelector('#submit');
const form = document.querySelector('#subscription');
const messageEl = document.querySelector('#message');

btn.addEventListener('click', (e) => {
    e.preventDefault();
    subscribe();
});

const subscribe = async () => {
    try {
        let response = await fetch('subscribe.php', {
        method: 'POST',
        body: new FormData(form),
        });
        const result = await response.json();

        showMessage(result.message, response.status == 200 ? 'success' : 'error');
    } catch (error) {
        showMessage(error.message, 'error');
    }
};

const showMessage = (message, type = 'success') => {
    messageEl.innerHTML = `
        <div class="alert alert-${type}">
        ${message}
        </div>
    `;
};
Code language: JavaScript (javascript)
Comment

PREVIOUS NEXT
Code Example
Javascript :: Get element inside iframe jQuery 
Javascript :: navigate between files in react js 
Javascript :: javascript compare two arrays of objects return difference 
Javascript :: save new 
Javascript :: unban command discord.js v12 
Javascript :: Copy an Array with the Spread Operator 
Javascript :: password parsley 
Javascript :: object.keys map 
Javascript :: disable click extra collapse antd 
Javascript :: jq unique by object attribute in list 
Javascript :: how to stop re rendering in react hooks 
Javascript :: how to do multiple pages in angular 
Javascript :: remove json parameter 
Javascript :: redux extension link 
Javascript :: exit from jshell 
Javascript :: how to generate password hash and a salt in nodejs 
Javascript :: SHOPIFY STORE FRONT PASSWORD 
Javascript :: Fix the transition judder at 0/60 seconds javascript30 js clock 
Javascript :: what is clz32 in javascript 
Javascript :: declerative and imperative program combine 
Javascript :: reindex api ealtic search 
Javascript :: jQuery - The noConflict() Method 
Javascript :: radio button form validation 
Javascript :: react with two components render 
Javascript :: Combine multiple JSONs Into One 
Javascript :: formart japanese date in js 
Javascript :: new date is not working in react js 
Javascript :: say something in console javascript 
Javascript :: make navigation open when items are active 
Javascript :: javascript get object list by value 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =