How to exclude undefined form elements from an HTML GET request

When you submit an HTML form with the = 'get' method, the values ​​from the form will be formatted into a GET request, for example

www.site.com/script.php?var1=value&var2=value&... 

As far as I can tell, if any of the elements in the form is not specified, they still fall into the string. If var1 in the above example was not specified, you will see ...

 www.site.com/script.php?var1=&var2=value&... 

Is there a way to make the form not include any unspecified values ​​in the GET request (preferably without javascript)?

+6
source share
1 answer

There is no need to do this. You can easily handle passed variables using PHP. But if you are really interested in this, you can use jQuery.

In any case, you can do something like this:

 <form action="index.html" method="get"> <input name="name"> <input name="name2"> <input name="name3"> <input type="submit"> </form> <script type="text/javascript"> $("form").submit(function() { $("form input").each(function(index, element) { if(($(this).val()=="")){ $(this).attr("disabled","disabled"); } }); }); </script> 

But remember that this is not a good practice!

+2
source

Source: https://habr.com/ru/post/913894/


All Articles