Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling out a form using javascript

I want to fill out a form when opening a page (it's a page where you edit your profile information).

Here is my code:

<head>
    <script type="text/javascript">
        function addValues() {
            document.getElementById("datePicker").value = ViewBag.b.DateOfBirth.ToShortDateString();
            document.getElementById("name").value = ViewBag.b.Username;
            document.getElementById("username").value = ViewBag.b.Username;
            document.getElementById("password").value = ViewBag.b.Password;
            document.getElementById("lastname").value = ViewBag.b.Lastname;
        }
    </script>
</head>




<body onload="addValues()">
    <h2>EditProfile</h2>
    <form action="~/Authentication/EditProfile" method="post">
        <table>
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" id="username" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="text" name="password" /></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><input type="text" name="name" id="name" /></td>
            </tr>
            <tr>
                <td>Last name:</td>
                <td><input type="text" name="lastname" /></td>
            </tr>
            <tr>
                <td>Date of birth:</td>
                <td><input type="date" name="dateofbirth" id="datePicker" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Save" />
                </td>
            </tr>
        </table>
    </form>
</body>

When loading a page, it doesn't fill out the information, and I can't find the mistake.

Is there a better way of doing this?

The rendered JavaScript looks like:

function addValues() {
    document.getElementById("datePicker").value = 11.9.2001. 00:00:00;
    document.getElementById("name").value = Mirko;
    document.getElementById("username").value = micro;
    document.getElementById("password").value = 123456789;
    document.getElementById("lastname").value = Mijanovic;
}
like image 991
Nikola Lugumerski Avatar asked Feb 01 '26 03:02

Nikola Lugumerski


1 Answers

You missed setting the id for some of the <input/> elements.

<head>
  <script type="text/javascript">
    // Example data
    const ViewBag = {
      b: {
        DateOfBirth: '2020-09-30',
        Username: 'username',
        Password: 'password',
        Lastname: 'lastname'
      }
    };

    function addValues() {
      document.getElementById("datePicker").value = ViewBag.b.DateOfBirth;
      document.getElementById("name").value = ViewBag.b.Username;
      document.getElementById("username").value = ViewBag.b.Username;
      document.getElementById("password").value = ViewBag.b.Password;
      document.getElementById("lastname").value = ViewBag.b.Lastname;
    }
  </script>
</head>


<body onload="addValues()">
  <h2>EditProfile</h2>
  <form action="~/Authentication/EditProfile" method="post">
    <table>
      <tr>
        <td>Username:</td>
        <td><input type="text" name="username" id="username" /></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="text" name="password" id="password" /></td>
      </tr>
      <tr>
        <td>Name:</td>
        <td><input type="text" name="name" id="name" /></td>
      </tr>
      <tr>
        <td>Last name:</td>
        <td><input type="text" name="lastname" id="lastname" /></td>
      </tr>
      <tr>
        <td>Date of birth:</td>
        <td><input type="date" name="dateofbirth" id="datePicker" /></td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" value="Save" />
        </td>
      </tr>
    </table>
  </form>
</body>

</html>

Hope this helps,

like image 66
Miroslav Glamuzina Avatar answered Feb 02 '26 16:02

Miroslav Glamuzina