Wallamic
Wallamic

Reputation: 1

Clear HTML Form after Submit with Javascript js

I've looked at other threads and cannot find a solution but I have a form that is submitting to a Google Sheet properly but the inputs retain content after pressing submit.

The code is below:

<form name="rvcasole-formresponses">

  <p class="text name">FIRST NAME</p>
  <input type="text" name="firstName" class="_input firstName">

  <p class="text name">LAST NAME</p>
    <input type="text" name="lastName" class="_input lastName">

  <p class="text email">EMAIL</p>
    <input type="text" name="email" class="_input email">

  <p class="text phone">PHONE</p>
    <input type="text" name="phone" class="_input phone">

  <button type="submit" class="button">SUBMIT</button>
</form>

<script>  
  const scriptURL = 'https://script.google.com/macros/s/AKfycbydQrV11QKRWOJ-HPTlEsVaZa1Z3z15DAN6It5k42b8voWDO4w/exec';
  const form = document.forms['rvcasole-formresponses'];
  form.addEventListener('submit', e => {  
   e.preventDefault();
   fetch(scriptURL, { method: 'POST', body: new FormData(form)})  
    .then(response => console.log('Success!', response))  
    .catch(error => console.error('Error!', error.message));
  })  
 </script>  

Upvotes: 0

Views: 387

Answers (3)

SantoshK
SantoshK

Reputation: 1877

we can reset the form by using document.getElementById('formresponses').reset()

const scriptURL = 'https://script.google.com/macros/s/AKfycbydQrV11QKRWOJ-HPTlEsVaZa1Z3z15DAN6It5k42b8voWDO4w/exec'  
  const form = document.forms['rvcasole-formresponses']  
  form.addEventListener('submit', e => {  
   e.preventDefault()  
   fetch(scriptURL, { method: 'POST', body: new FormData(form)})  
    .then(response => document.getElementById('formresponses').reset())  
    .catch(error => console.error('Error!', error.message))  
  }) 
<form name="rvcasole-formresponses" id="formresponses">

    <p class="text name">FIRST NAME</p>
    <input type="text" name="firstName" class="_input firstName">

  <p class="text name">LAST NAME</p>
    <input type="text" name="lastName" class="_input lastName">

  <p class="text email">EMAIL</p>
    <input type="text" name="email" class="_input email">

  <p class="text phone">PHONE</p>
    <input type="text" name="phone" class="_input phone">

  <button type="submit" class="button">SUBMIT</button>
</form>

Upvotes: 1

Sumesh TG
Sumesh TG

Reputation: 2575

<form name="rvcasole-formresponses">

    <p class="text name">FIRST NAME</p>
    <input type="text" name="firstName" class="_input firstName">

    <p class="text name">LAST NAME</p>
    <input type="text" name="lastName" class="_input lastName">

    <p class="text email">EMAIL</p>
    <input type="text" name="email" class="_input email">

    <p class="text phone">PHONE</p>
    <input type="text" name="phone" class="_input phone">

    <button type="submit" class="button">SUBMIT</button>
</form>

<script>
    const scriptURL = 'https://script.google.com/macros/s/AKfycbydQrV11QKRWOJ-HPTlEsVaZa1Z3z15DAN6It5k42b8voWDO4w/exec'
    const form = document.forms['rvcasole-formresponses']
    form.addEventListener('submit', e => {
        e.preventDefault()
        fetch(scriptURL, {
                method: 'POST',
                body: new FormData(form)
            })
            .then(function(response) {
                var frm = document.getElementsByName('rvcasole-formresponses')[0];
                frm.reset();
                console.log('Success!', response);
            })
            .catch(error => console.error('Error!', error.message))
    })
</script>

After success clear the form using form selector.

Upvotes: 0

Mandar Dhadve
Mandar Dhadve

Reputation: 381

just add this :

Add form: id="myForm"

        document.getElementById("myForm").reset();

Upvotes: 0

Related Questions