Prefill html form with element without ID - html

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>

Related

Redirect to selected site with variable which i got

<form action="/subsite/" method="GET">
<input type="text" name="" placeholder="Your Nick">
<input class="button" type="submit" />
</form>
I want to redirect I mean it should looks like
www/subsite/text
What should I use ? POST ?
The method="get" means that the parameters (and values) will be sent in the query string (the stuff after the question mark in the URL). eg.
/subsite?input_field_name=input_field_value
In your case, the input field doesn't have a name, which will cause problems. You probably want something like this:
<input type="text" name="nickname" placeholder="Your Nick">
So if you submit this form:
<form action="/subsite" method="get">
<input type="text" name="nickname" placeholder="Your Nick">
</form>
Then after submitting, the browser will go to:
/subsite?nickname=value_of_nickname_variable
If you use a method="post", then the form data (variables) will be sent along in the request body, not the query string. There are other differences between get/post, but that's one of them :)
If you just want to do a simple redirect when clicking the button, you could use javascript instead, eg.: window.location.href='/my_url_path_here'

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.

Input outside of multiple form containers

http://www.w3schools.com/tags/att_select_form.asp
So in the link above, it shows that an <input> (select) can be outside of the <form> container, as long as the form is reference.
However, what if I have multiple forms and I would like to make sure that the value from <select> is included in the form that is submitted. How would I go doing that?
An example:
<select id="item1">....</select>
<form id="form1">...</form>
<form id="form2">...</form>
<form id="form3">...</form>
<form id="form4">...</form>
I want to make it so that no matter which form is submitted, item1 is always included.
you can do something like this
<select id="item1" name="item1">....</select>
<form id="form1" onsubmit="document.getElementById('h1').value = document.getElementById('item1').value">
<input type=hidden value="" id="h1" name="item1">
</form>
Using a javascript onsubmit, such as is provided in this answer, may be useful to get the data and append it to your form, regardless of the submitted form.

How to change the url sent to an html form?

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