How to change the url sent to an html form? - html

Suppose we have:
<form action="xxx">
<input type="checkbox" id = "checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>"
When click the submit button, the url should be something like "xxx?checked=1". In this case, I want to append another parameter to the url. I know using hidden value is an option, but do we have a more direct way? Please suggest. Thanks

The fact is the url depends on the method you are using to send the form. This is set on the method attribute on the <form> tag.
The URL of the action can be any valid URL so to add extra attributes like something=1 and other=2 you could set the following:
<form action="xxx?something=1&other=2">
<input type="checkbox" id="checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>
submitting the form will now send the GET request xxx?something=1&other=2&checked=1

Related

Prefill html form with element without ID

I have a form:
<form action="#" method="post" class="aui" id="advanced-settings-form">
<input class="text medium-long-field" type="text" name="6" placeholder="/**/*">
</form>
I know that it is possible to prefill the form via url as: URL?id=text
But in my case there is no id - only a name.
Is it still possible to prefill the form via URL?
Well, for sending data through a form, we use name attribute and not id attribute. What you are looking for is to use value attribute of <input> tag.
Also URL?id=text which you have mentioned, means that the form has already been submitted usingmethod=get. It has no relation with prefilling of the input boxes.
Something like this:-
<form action="#" method="post" class="aui" id="advanced-settings-form">
<input class="text medium-long-field" type="text" name="name_here" placeholder="/**/*" value="text">
</form>

Get name of the form in servlet

<form name="GetAll"
method="post"
action="NewServlet">
<input type="text" name="des">
<input type="submit">
</form>
How I can get name of this form (GetAll) in my NewServlet servlet?
The name attribute of the form is not part of the HTTP request when it is submitted. You could add a hidden field instead to identify multiple forms off same page like
<input type="hidden" name="formName" value="formA">
keeping the name attribute of the hidden input same of course.

How to build a custom URL from a HTML form?

I have a HTML form like:
<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
I need to build a URL when the submit link is clicked and send the user to it, using the username from the form in the correct position in the URL.
URL:
http://example.com/htmlchat/init.html?init_room=1&init_user=USERNAME_FROM_FORM_HERE
Can someone help (an example would be great)?
Don't use POST when you want the data to appear in the URL
Do put all the data you want in your URL in the form
Get the names of your fields right
Put the base URI in the action
such:
<form action="init.html">
<input type="hidden" name="init_room" value="1">
<label>
Username:
<input name="init_user">
</label>
<input type="submit" value="Submit">
</form>
This is possible with either Javascript or a server side language such as PHP. With Javascript you want to update the action attribute.
You could use jQuery to do this.

How to name form elements to submit as array in URL?

I am creating a page that will allow results to be filtered. I am trying to create a URL that looks something like this, once the form is submitted:
/results/filtered?categories=[1,3,5]&types=[7,8,9]
I have a form that looks something like this:
<form action="/results/filtered" method="get">
Category filters:
<input type="checkbox" name="categories[]" value="1">
...
<input type="checkbox" name="categories[]" value="5">
Types:
... same sort of thing
<input type="checkbox" name="types[]" value="9">
<input type="submit" value="Go">
</form>
However, when I submit the form, I'm getting a URL that looks like:
/results/filtered?categories%5B%5D=1&categories%5B%5D=3&categories%5B%5D=5&types%5B%5D=7&types%5B%5D=8&types%5B%5D=9
It works, but it's pretty ugly. How can I change my form so that I get nice clean URLs?
You can't control the format of the url that way unless you create your own form pre-processor using javascript.

How to forward data from one jsp A to jsp B and then all data to final servlet?

I have two JSP pages A and B and also a Servlet.
The flow is like this:
User fill in some information in page A
Then the user fill in some other information in page B
After the user finishes B, he/she will click a button and submit all data (from both A and B) to the Servlet.
How should I design this?
My plan is
In A's next button, it is actually a <a> tag with link to the href of B. All information from A should be passed to B via that link. I don't know how to do this step.
In B's finish button, it is a form input submit button. I don't know whether I can or how I add A's data into this form.
Anyone can help me out?
Thanks
In your A.jsp create a link, for e.g.: Go to B
Pass your parameters from URL.
In B.jsp use expression language to get parameters values:
<form action="FinalServlet" method="post">
<input type="text" name="p" value="your value"/>
<input type="hidden" name="p1" value="${param.param1}"/>
<input type="hidden" name="p2" value="${param.param2}"/>
.......
<input type="submit" value="Finish"/>
</form>
Hope this helps.
EDIT:
If you want to use a form with input fields in A.jsp:
<form action="B.jsp" method="post">
<input type="text" name="param1" value="value1"/>
<input type="text" name="param2" value="value2"/>
.......
<input type="submit" value="Finish"/>
</form>
You will receive parameters in B.jsp using the same EL.