html form input method get prevent name in url if value is not filled

anamul

Let's say this is my html in reactjs/jsx:

<form action="/search-list" method="GET">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" value="Submit">
</form>

and when i click on the submit buttoin without inputing anything, it make the url like this fname=&lname=

and when i fill anly fname and submit, the url become like this fname=JhonDoe&lname=

Notice here, i didn't fill the lname, yet the lname is here in url which i dont want.

I want, if a field has value, that field should be in url as query params, if not field, that field should not be in url query params.

is it possible to do? how can I do it? All the fields should be optional.

Mehrwarz

You can use this plain JavaScript to remove the empty inputs from submitting.

<form method="GET" id="myForm">
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname"><br><br>
    <input type="submit" value="Submit">
</form>

<script>
    Form = document.getElementById("myForm");
    Form.addEventListener('submit',()=>{
    if(Form.fname.value == ""){
    Form.fname.name ="";
    }

    if(Form.lname.value == ""){
    Form.lname.name ="";
    }


    Form.submit();
    });

</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to prevent a form submit if some of the input areas are not filled

From Dev

Get value form input

From Dev

HTML form not submitting input values with post/get method

From Dev

File_get_html url variable from input search FORM

From Dev

HTML Form Input initial, focus and filled out states

From Dev

html Input field is auto filled with username saved in browser form

From Dev

HTML Form won't submit when text input is filled

From Dev

How to get month name from an html Date input value?

From Dev

How to get the value inside a input name in html using php

From Dev

HTML - Input-form for URL

From Dev

How Can Get Html Form value view to Controller Action Method

From Dev

redux-form get the name of form for the input

From Dev

get input form not as variable but as url

From Dev

get pure name form an URL

From Dev

prevent form submission if form fields are not filled

From Dev

How to get html source of current website with filled input values

From Dev

Get form input value with Dart

From Dev

How to get form input value?

From Dev

Prevent a Form Input value from being submitted in the form

From Dev

Is it possible to get form input into another html form

From Dev

Finding URL on which the form was filled

From Dev

remove the ? and name= in get method html

From Dev

jQuery - Change name of an input field by using value of another input field filled at the same time

From Dev

Get and pass value of input by url

From Dev

Can the name attribute of the input of the HTML form not have the same value as the type attribute?

From Dev

Prevent HTML to be displayed from $_GET method

From Dev

download filled html form as pdf

From Dev

How to get form input fields if name is not known

From Dev

How can I make responsive html table fields which are automatically filled in by user's input in an html form?

Related Related

  1. 1

    How to prevent a form submit if some of the input areas are not filled

  2. 2

    Get value form input

  3. 3

    HTML form not submitting input values with post/get method

  4. 4

    File_get_html url variable from input search FORM

  5. 5

    HTML Form Input initial, focus and filled out states

  6. 6

    html Input field is auto filled with username saved in browser form

  7. 7

    HTML Form won't submit when text input is filled

  8. 8

    How to get month name from an html Date input value?

  9. 9

    How to get the value inside a input name in html using php

  10. 10

    HTML - Input-form for URL

  11. 11

    How Can Get Html Form value view to Controller Action Method

  12. 12

    redux-form get the name of form for the input

  13. 13

    get input form not as variable but as url

  14. 14

    get pure name form an URL

  15. 15

    prevent form submission if form fields are not filled

  16. 16

    How to get html source of current website with filled input values

  17. 17

    Get form input value with Dart

  18. 18

    How to get form input value?

  19. 19

    Prevent a Form Input value from being submitted in the form

  20. 20

    Is it possible to get form input into another html form

  21. 21

    Finding URL on which the form was filled

  22. 22

    remove the ? and name= in get method html

  23. 23

    jQuery - Change name of an input field by using value of another input field filled at the same time

  24. 24

    Get and pass value of input by url

  25. 25

    Can the name attribute of the input of the HTML form not have the same value as the type attribute?

  26. 26

    Prevent HTML to be displayed from $_GET method

  27. 27

    download filled html form as pdf

  28. 28

    How to get form input fields if name is not known

  29. 29

    How can I make responsive html table fields which are automatically filled in by user's input in an html form?

HotTag

Archive