My html form automatically passing all field data in url

Viewed 33

I have created a simple html form but I have encountered a weird problem which I have never seen before. When I click on submit then all of the data is directly passing to the URL bar. I don't know what is wrong with this can anyone help?

Form:


      <form id="form-data">
          Name<br><input type="text" name="name" id="name" value=""><br><br>
          Age<br><input type="number" name="number" id="age" value=""><br><br>
          Gender<br>
            <input type="radio" name="gender" value="Male">Male
            <input type="radio" name="gender" value="Female">Fe-male<br><br>
    
          <select name="country">
            <option value="Kashmir">Kashmir</option>
            <option value="Egypt">Egypt</option>
            <option value="Norway">Norway</option>
            <option value="Iceland">Iceland</option>
          </select><br>
          <br><input type="submit" id="submit" value="Save">
      </form>

3 Answers

the form uses get method therefore it displays the form values in the url. change the method to post to avoid this

method="post"

<!DOCTYPE html>
<html>
<head>
    <title></title>
<body>
  <form id="form-data" method="post">
      Name<br><input type="text" name="name" id="name" value=""><br><br>
      Age<br><input type="number" name="number" id="age" value=""><br><br>
      Gender<br>
        <input type="radio" name="gender" value="Male">Male
        <input type="radio" name="gender" value="Female">Fe-male<br><br>

      <select name="country">
        <option value="Kashmir">Kashmir</option>
        <option value="Egypt">Egypt</option>
        <option value="Norway">Norway</option>
        <option value="Iceland">Iceland</option>
      </select><br>
      <br><input type="submit" id="submit" value="Save">
  </form>

</body>
</html>

If you don't send the data somewhere, it gets passed as URL parameters to the current page: From MDN Web Docs:

The action attribute defines where the data gets sent. Its value must be a valid relative or absolute URL. If this attribute isn't provided, the data will be sent to the URL of the page containing the form — the current page.

Related