0

I have in file JSON data which I'm displaying on UI and a form from which user can update data. How can I update the JSON data with the values entered by the user on the form and render it on the UI after submit.

 //Html code
 <div id="myModal" class="modal">
   <!-- Add movie Modal content -->
   <div class="modal-content">
     <span class="close" onclick="closeModal()">&times;</span>
     <form name="submitmovie" onsubmit="addMov()">
       <label>Enter Movie Name</label>
       <input type="text" name="movie-name" id="mnane" placeholder="enter movie name">
       <label>Year</label>
       <input type ="number" name="movie-year" id="myear" placeholder="movie-year">
       <label>Poster Url</label>
       <input type ="url" name="movie-poster" id="mposter" placeholder="movie-poster-url">
        <button class="addMovie" type = "submit">Submit</button>
      </form>
    </div>
  </div>

  <script type="text/javascript">
    function addMov(){
      var name = document.getElementById("mnane").value;
      var year = document.getElementById("myear").value;
      var url = document.getElementById("mposter").value;
      console.log(name,year,url);
      // add();
      movieList.push({
        "name":name,
        "year":year,
        "url":url
      });
      renderMovieCards();
    }
  </script>
3
  • Fetch. Commented Apr 13, 2019 at 11:17
  • The browser has limited authorization when it comes to creating files on the user's machine. You would need yo use a server to manipulate your JSON file, or just switch to a different format, like localStorage(which would be relevant only in a given browser) Commented Apr 13, 2019 at 11:36
  • Possible duplicate of How to write data to a JSON file using Javascript Commented Apr 13, 2019 at 16:30

1 Answer 1

0

To perform update on a JSON file you will need a backend or some third party product that act like backend eg. Firebase.

If we talking just locally (changes persist on page refresh) what you can is:

<div id="myModal" class="modal">
  <!-- Add movie Modal content -->
  <div class="modal-content">
    <span class="close" onclick="closeModal()">&times;</span>
    <form name="submitmovie" onsubmit="addMov()">
      <label>Enter Movie Name</label>
      <input type="text" name="movie-name" id="mnane" placeholder="enter movie name">
      <label>Year</label>
      <input type="number" name="movie-year" id="myear" placeholder="movie-year">
      <label>Poster Url</label>
      <input type="url" name="movie-poster" id="mposter" placeholder="movie-poster-url">
      <button class="addMovie" type="submit">Submit</button>
    </form>
  </div>
</div>

<script type="text/javascript">
  // persist mechanics
  // on app load try to load data from localStorage
  const movieList = localStorage.getItem('movieList');
  // if it's there assign it to moveList, else leave it as is (with json data)
  if (movieListLocalStorage) {
    movieListLocalStorage = JSON.parse(movieListLocalStorage);
  }
  function addMov() {
    var name = document.getElementById("mnane").value;
    var year = document.getElementById("myear").value;
    var url = document.getElementById("mposter").value;
    console.log(name, year, url);
    // add();
    movieList.push({
      "name": name,
      "year": year,
      "url": url
    });
    // since you are using movieList i am assuming it has predefined json data assigned
    // persist updated data within localStorage
    localStorage.setItem('movieList', JSON.stringify(movieList))

    // pass updated data to render function
    renderMovieCards(movieList);
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I Tried with way but still, values are not getting updated in the local storage. Here is my code pen link for the whole code. codepen.io/pb09/pen/ROZzer Can you please let me know where i'm doing wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.