Alpine: Setting component data using AJAX

Created on 10 May 2020  路  3Comments  路  Source: alpinejs/alpine

I'm submitting a form with AJAX and having trouble setting my component's success value.

Here's what I have so far:

<div x-show="subscribeVisible" x-data="{ subscribeVisible: false, success: false }">
  <form id="subscribe" x-ref="form">
      <input type="email" name="email" />
    <button @click.prevent="submitForm($refs)">Subscribe</button>
  </form>
  <div x-show="success">Form submitted!</div>
</div>

<script>
  function submitForm($refs) {
    axios.post($refs.form.action, new FormData($refs.form))
    .then(response => {
      console.log(response.data);
      return this.success === true
    });
  }
</script>

Most helpful comment

You need to make your x-data a function call

<div聽x-show="subscribeVisible"聽x-data="component()">
聽聽<form聽id="subscribe"聽x-ref="form">
聽聽聽聽聽聽<input聽type="email"聽name="email"聽/>
聽聽聽聽<button聽@click.prevent="submitForm($refs)">Subscribe</button>
聽聽</form>
聽聽<div聽x-show="success">Form聽submitted!</div>
</div>

<script>
  function component () {
    return {
      subscribeVisible:聽false,
      success:聽false,
      submitForm()聽{
聽聽聽聽    axios.post(this.$refs.form.action,聽new聽FormData(this.$refs.form))
聽聽聽聽    .then(response聽=>聽{
聽聽聽聽聽聽    console.log(response.data);
聽聽聽聽聽聽    return聽this.success聽===聽true
聽聽聽聽    });
聽聽    }
    }
  }
</script>

All 3 comments

You need to make your x-data a function call

<div聽x-show="subscribeVisible"聽x-data="component()">
聽聽<form聽id="subscribe"聽x-ref="form">
聽聽聽聽聽聽<input聽type="email"聽name="email"聽/>
聽聽聽聽<button聽@click.prevent="submitForm($refs)">Subscribe</button>
聽聽</form>
聽聽<div聽x-show="success">Form聽submitted!</div>
</div>

<script>
  function component () {
    return {
      subscribeVisible:聽false,
      success:聽false,
      submitForm()聽{
聽聽聽聽    axios.post(this.$refs.form.action,聽new聽FormData(this.$refs.form))
聽聽聽聽    .then(response聽=>聽{
聽聽聽聽聽聽    console.log(response.data);
聽聽聽聽聽聽    return聽this.success聽===聽true
聽聽聽聽    });
聽聽    }
    }
  }
</script>

Thanks so much. That did it. Here's the final solution:

<script>
  function subscribe () {
    return {
      errors: [],
      subscribeVisible: false,
      success: false,
      submitForm() {
        axios.post(this.$refs.form.action, new FormData(this.$refs.form))
        .then(response => {
          if (response.data.success) {
            this.errors = [];
            this.success = true;
            this.$refs.form.reset();
          }
        });
      }
    }
  }
</script>

:+1:
You probably want to disable the button on click and reenable it on error or success as well. It will save you from figuring out why you have duplicate subscriptions in the future (yeah some people double click buttons :joy:).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unlikenesses picture unlikenesses  路  5Comments

adevade picture adevade  路  3Comments

adinata-id picture adinata-id  路  4Comments

piotrpog picture piotrpog  路  3Comments

piotrpog picture piotrpog  路  3Comments