can value field of input in ejs be edited like this?

Viewed 723

I want to add value to the "value" filed of input dynamically but this is not working.

        <form action="/assignCol" method="POST">
        <input
        type="RefCode"
        id="RefCode"
        name="RefCode"
        class="form-control"
        value="<%= user[i].id %>"
        readonly="readonly"
      />
      </form>

my aim is to provide the user with a feature to update the details of a person just by clicking the update button. I want that once the user clicks the update button I could be able to send the user[i].id to the put API.

also, I want to send this user[i].id to the put API in the backend once I click this update button. so that in the backend I can fetch the data of that user with this id using req.body.id and find and edit the details of that user. is there any other possible way to do that?

2 Answers

If you are using server-side rendering you should use type="hidden" instead of type="RefCode".

An alternate approach is the Single Page Application (SPA). This approach removes the need for server-side rendering (no need for express to render HTML file on each request) and instead usage Ajax to and JSON API endpoints to implement the sight.

I would recommend Angular - Tour of Heroes if you like a taste of SPA.

If you want to use the values at backend side then you must access the body object of request using name of <input> which is here RefCode

so it must be req.body.RefCode, to call the passed value

Secondly, you asked for another possible way of doing this, for simplicity I am changing properties name

 
        <input
        type="RefCode"
        id="refcode"
        name="refcode"
        class="form-control"
        value="<%= user[i].id %>"
        readonly="readonly"
      />
      <button onclick="updateData()"</button>


<script>
     const updateData = async () =>{
      const userId= document.getElementById("refcode");
      const result= await fetch(url, {
      method:'PUT', // You can also use POST here
      body:userId
      }
      console.log(result)
     }

<script>
Related