Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jdubstep1357's avatar

How to keep dynamic data on webpage after refreshing - Alpine.js

I made a submit form in alpine.js that dynamically adds data. However, if I click on the refresh button, the data gets wiped out. How can I have it so that every time I refresh the browser after adding names, the data stays the same? I thought of using local storage, but had difficulty with figuring out how to implement it.

Here is my html:

<div class="container py-4" x-data="addRemove()">
  <hr>
  <form x-on:submit.stop.prevent="addRemoveSubmit">
  <div class="row">
    <div class="col">
      <div class="form-group">
        <label for="first">First</label>
        <input type="text" class="form-control" id="text" aria-describedby="text" x-model="newFirst">
      </div>
    </div>
    <div class="col">
      <div class="form-group">
        <label for="last">Last</label>
        <input type="text" class="form-control" id="last" aria-describedby="last" x-model="newLast">
      </div>
    </div>
    <div class="col-2 d-flex align-items-end">
      <div class="form-group col px-0">
        <button type="submit" class="btn btn-primary btn-block">Add</button>
      </div>
    </div>
  </div>
  </form>
  <table class="table table-bordered mt-2 mb-4">
    <thead>
      <tr>
        <th scope="col">First</th>
        <th scope="col">Last</th>
        <th scope="col">Action</th>
      </tr>
    </thead>
    <tbody>
      <template x-for="(person, index) in Object.values(persons)" :key="index">
        <tr>
          <td x-text="person.first"></td>
          <td x-text="person.last"></td>
          <td><button x-on:click="persons = persons.filter(p => p.id !== person.id)" class="btn btn-danger btn-block">Remove</button></td>
        </tr>
      </template>
    </tbody>
  </table>
</div>
            

Here is my Javascript:

function randId() {
  return (Math.random() * 100).toFixed(0);
}

function addRemove() {
  return { 
    newFirst: '', 
    newLast: '', 
    persons: '', 
    addRemoveSubmit() {
      this.persons = [].concat({ 
        id: randId(), 
        first: this.newFirst, 
        last: this.newLast
      }, this.persons); this.newFirst = '', this.newLast = '';
    }
  }
}
0 likes
3 replies
jlrdw's avatar

I did not read all of that code, but learn how to refresh the dom.

Just a suggestion, take some of the basic JavaScript and Ajax tutorials to learn that sort of thing first.

Of course you realize you have to update this on the back end first, that's part of refreshing the Dom.

Jdubstep1357's avatar

@jlrdw so Ajax CAN work well with alpine. I wasn’t sure if doing things on top of alpine would be an issue.

jlrdw's avatar

@Jdubstep1357 if you add a new row of data that has to be inserted into the database:

You have to insert it on back end and "reload" that part of the DOM if you expect it to be there.

I have no idea about Alpine js, but I know the DOM.

Edit:

Just a suggestion, before dabbling around in alpine, take some basic how the DOM works tutorials.

Please or to participate in this conversation.