How to hide parameters in URL without javascript? - html

My form is like this:
<form method="GET" action="target">
<input type="text" name="filter"/>
<select name="skill">
<option>opt1</option>
<option>opt2</option>
<option>opt3</option>
</select>
<input type="number" name="level"/>
<input type="submit"/>
</form>
I would like the empty parameters not to be shown in the URL when the form is submitted, and this happens with the skill field , but not with level and filter, which are added to the Query string even though they're empty. How does it happen? Is it possible to prevent this from happening without using javascript?
None of these fields is required to submit the form.

This is not possible without javascript.
You can use something like jQuery to accomplish this.
note: Using form method POST is the preferred way of submitting forms, and the parameters are submitted as http request body and not in the URL.

If you do a form POST instead of GET, the form params will not appears in the query string. But that would require back end changes.

Related

Sending additional parameters via form using POST: hidden fields or query parameters

I have some HTML forms and I need to provide additional parameters which the user will not fill up in the form UI. I understand I can do it using query parameters or hidden form fields. Examples :
a form whose data must be submitted to the server together with an option chosen in the previous web page
Ex: URL /createImage?option=option_value (accessible via GET by user) would contain the HTML code below
Query parameter
<form action='/createImage?option=option_value'>
<input type='textfield1' name='var1'>
</form>
Hidden field
<form action='/createImage'>
<input type='hidden' name='option' value='option_value'>
<input type='textfield1' name='var1'>
</form>
(in this case, I suppose a third solution would be to use a HTTP session variable instead)
a fake form used to associate a POST HTTP request with a image.
Query parameter
<form action='/createContainer?image=image1'>
<input type='image' src='container.png'>
</form>
Hidden field
<form action='/createContainer'>
<input type='hidden' name='image' value='image1'>
<input type='image' src='container.png'>
</form>
(in this case, I suppose a third solution would be to use a standard img HTML tag and use Javascript to send the POST request to the server on its click() event)
Are there any upsides/downsides in each one of those approaches or is it just a matter of personal preference?

HTML form that causes a GET request

Basic question. If I have a form in my HTML (where in my case someone inputs a date), how can I have my users cause a GET request with the contents of that form instead of a POST.
e.g. form entry (e.g date)... so 20190312
what I am trying to achieve is such that AFTER the user submits the form.. the user is the lead to page that has
GET domain.tld/scriptname.php?variable=20190312
and then the system then processes the GET request accordingly.
thank you
Maybe i'm missunderstanding what you are asking.
This can easly be achived using builtin GET method in FORM tag
<body>
<form id="form" method="GET" action="scriptname.php">
<input id="date-txt" type="text" name="date">
<input id="search-btn" type="submit" value="Submit">
</form>
</body>
While filling up above field ad clicking "Submit" form will be submitted and you can see in your url path/or/page/scriptname.php?date=INPUT_FIELD_VAL
for every input in #form with a name, if GET method is used, you'll see a ?name=value in the url
What you describe is the default behaviour of a form. If you don't want a POST request, then don't use a method attribute to set the request type to POST.
<form action="//domain.tld/scriptname.php">
<input name="variable" value="20190312">
<button>Submit</button>
</form>

Any way to force a form to submit fields differently without using Javascript?

I have a form:
<form method="GET">
<input type="text" value="hello" name="myname" />
</form>
If this form is submitted, I will end up at:
example.com/?myname=hello
What I would prefer is that when this gets submitted, I end up at:
example.com/hello
Is this possible?
No, you cannot change the way form submission works in HTML. (Using JavaScript, you can do transactions in a different way, without using HTML form submission.) When using method="GET", the URL gets constructed in a specific way; when using method="POST", the URL does not contain submitted data at all (it is sent outside the URL).
There is a trick that changes form submission in one way, but not quite the way you want. If the name of a control is isindex, then the control name and the equals sign are omitted; but the question mark is still there. That is, <input type="text" value="hello" name="isindex" /> would result in http://www.example.com/?hello. And Chrome has broken this when they removed the remainders of support to the isindex element.
If, for some special reason, you really need to make a form create requests like http://example.com/hello, then the simplest way is to set up a very simple server-side script that accepts normal requests that result from HTML forms and just passes them forward after modifying the URL in a simple way.

Form submission without parameter name

<form action="/foo">
<input type="text" name="q">
<input type="submit" >
</form>
In the previous HTML, if the user types in 'bar' and clicks the button it would submit to example.com/foo?q=bar.
How can I make it go to example.com/foo/bar?
I can easily do it with JS but I'm trying to stick to HTML.
There is no way to do this in HTML, you need a programming language.
Have something on the server process the form an issue an HTTP 301 response with a Location header.
You could enhance that by use the form's submit event to prevent the default action and set location in JS.
You cannot change the value of the action attribute with HTML only. You need to use JS or any other scripting language.

Passing arguments to POST Method

Is that possible to pass arguments to a HTML form in POST method by modifying the URL? If so how can I do it?
Arguments in the URL will be passed via the GET method. To pass via POST, you need to construct and post a form.
If you wish to dynamically add fields to a form, you can do this by creating a new hidden input element in Javascript and appending it to your form. JSFiddle demo here.
No, that's impossible.
The GET method is passed via the URL. The POST method is sent using a form. You can in fact use both at the same time.
no, that's not possible.
"The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body."
http://www.cs.tut.fi/~jkorpela/forms/methods.html
You can do something like this:
<form method="POST" action="http://example.com?this=that">
<input type="text" name="textfield">
</form>
and retrieve the this=that value server-side. But generally speaking, mixing GET queries in a url with a POSTed form is generally frowned on. Especially if you've got duplicated field names in the url AND the form body.
I strongly recommend using a javascript library, such as jquery but here's the old school:
<script type="text/javascript">
function fillForm() {
document.getElementById('myText').setAttribute('value', 'some value for myText');
// this would send the form:
// document.getElementById('myForm').submit();
return true;
}
</script>
<form method="POST" action="formHandler.php" id="myForm">
<input type="text" id="myText" name="myText" value="" />
<input type="submit" value="Send" />
</form>
<button onclick="fillForm();">Fill it!</button>
There is a way in which you could pass arguments to POST method in bash(command line of course). Use wget tool. example :- wget somewebsite.com/login --post-data="username=name&password=password"