Why does the output differs in the given html code? - html

When i open the html file with the code snippet, mentioned below, in my browser i see a checkbox and a submit button.
<form >
<input type = "checkbox" name = "q">
<input type = "submit">
</form>
However, when I move the first line along with the form(as shown in the code snippet below) and then reload the file in my browser, I don't see the checkbox. I see only the submit button.
I infer that mentioning the first line along with form keyword means something different. Can someone explain what is the difference ?
<form input type = "checkbox" name = "q">
<input type = "submit">
</form>
PS: I am new to html and web development. This may be a noob question.

As per definition, the <form> tag is used to create an HTML form for user input. This form contains other elements such as <input>, <button>, <textarea>, etc. These elements cannot be clubbed into the form tag.
When you write :
<form input type = "checkbox" name = "q">
it creates an element form with attributes type="checkbox", name="q" and input="" and therefore it doesn't create the checkbox you wanted.
The correct format is :
<form>
<!-- form content -->
</form>

A form and an input are two different kinds of elements. You can't just merge them together, it makes no sense. If you want an input, you have to have an input element
For a bit of explanation, when you open a tag like so:
<form
everything you type within thhat tag before it is closed by a '>' is usually a property of that element. E.g.
<form prop1='a' prop2='b'>
blah blah
</form>
Such properties would things like classes or ids. An element can't be a property.

Related

How to send textarea value as a parameter in a POST request

I am trying send the value of a textarea using a Post method when a button is clicked.
Code is very simple:
<html>
<head>
</head>
<body>
<form action="/editFile" name="confirmationForm" method="post">
<textarea id="confirmationText" class="text" cols="86" rows ="20" name="confirmationText" form="confirmationForm">
<out value="#{user.getFileContent}"/>
</textarea>
<input type="submit" value="Email" class="submitButton" id=""/>
</form>
</body>
</html>
To get this data when this request is done I am using CWF web framework and the methode is very simple:
void editFileController::doPost(CWF::Request &request, CWF::Response &response) const
{
QString out = request.getParameter("confirmationText");//this will give me the value of the widget "confirmationText" from HTML
QString out1 = request.getParameter("confirmationForm");
}
In order to get value of a filed this is done in this way:
<input type="file" name="test"/>
request.getPatameter("test"); //all works ok
But for the first example (the one with textarea) I can't set it to send the value of textarea when button is pushed.
Can anyone give me some ides about how I can fix this? This frameworks know only to give me the value for a specific name. So, somehow I should set the value of the button with the value of the textarea when this is pushed.
Thank you.
The textarea needs to belong to the form you are submitting.
Since you have form="confirmationForm", it belongs to the form with id="confirmationForm" which doesn't exist.
Remove the form attribute from the <textarea> start tag.
Your form has name="confirmationForm". The name attribute served a similar purpose to the id attribute before HTML standardized on id for client-side references to elements when HTML 4 came out in the late 1990s.
Two decades later, there is no reason to give elements name attributes for client-side purposes.
(Giving form controls like <textarea> name attributes for the purposes of submitting data for server-side use is still correct).

What does the fieldset element's name attribute do?

I understand the general semantics of a <fieldset>, along with how the name attribute on a <fieldset> can be used to give meaning to a group of inputs. The W3 wiki has a number of examples with this.
However, I don't understand what the name attribute on <fieldset> elements do when submitting forms. According to MDN, the name is submitted with the form data. W3C also mentions that it's for the element's name, which is used in form submission.
When trying out the name attribute on a <fieldset> though, I don't see it being submitted with the rest of the form data. It's unclear to me if it has any use other than semantics.
Is the name attribute on <fieldset> elements supposed to be submitted with the form data? If so, does it have a value? What does it do?
However, I don't understand what the name attribute on <fieldset> elements do when submitting forms.
Nothing.
You've tested this yourself:
When trying out the name attribute on a <fieldset> though, I don't see it being submitted with the rest of the form data.
It appears to be an error in the summary of the spec (presumably copied from input at some point) which has been copied onto the MDN reference.
The rules for form submission don't mention fieldsets.
W3C also mentions that it's for the element's name, which is used in form submission.
The complete quote is:
name - Name of form control to use for form submission and in the form.elements API
The form.elements API (along with generic DOM APIs like getElementsbyName) is the only place where the attribute has any actual effect.
This is a bit strange to me, but it seems the purpose is so that you can access it via myForm.elements like you can any other form element. Fieldsets do have some relevance to how your form functions (for example, making a fieldset "disabled" will apply to all its child form controls), so I guess this is something some developer might want to do.
I'm pretty sure that's the only use for it. Fieldsets can't have a value (even if you set one, your browser should ignore it and not submit it), so it'll never be included in what is sent to the server.
Here's a little test I made trying to understand this:
var fruitform = document.getElementById("fruitform");
var output = document.getElementById("output");
function log(msg) {
output.innerHTML = output.innerHTML + "\n" + msg;
}
log("*** fruitform.elements ***")
log(JSON.stringify(fruitform.elements));
log("");
<h2>test form</h2>
<!-- the 'action' url' is just a page that outputs any GET parameters you pass to it -->
<form action="http://www.w3schools.com/html/action_page.php" id="fruitform">
<fieldset name="facts">
type:
<input type="text" name="type" value="banana">
<br><br>
color:
<input type="text" name="color" value="yellow">
</fieldset>
<br>
<input type="submit">
</form>
<!-- place to put some output -->
<h2>output</h2>
<pre id="output">
</pre>
(here's the same demo on codepen)
I agree with #Quentin .
The fieldset seems to not even be sent to the server, just the input field:

Why is the submit button value passed when using the GET method in HTML?

I am using the the "GET" method in a form on my website. For some reason it is passing the value of the submit button to the url. Why is this happening? What am I doing wrong?
Form:
<form method="GET" action="searcht1.php">
<input type="text" name="search"/>
<input type="submit" name="submit">
</form>
Url:
searcht1.php?search=colin+pacelli&submit=Submit
It's supposed to happen. If you don't want that, do not define name attribute on the button. You probably want value instead, to show the user what the button is for.
Also, this question has nothing to do with PHP; it is purely about HTML semantics.
The reason is that the name attribute makes the submit button a “successful control” (in HTML 4.01 terminology) when it is used for form submission. This causes the name=value pair from it to be included in the form data.
Note that in your case, this data is name=foo where foo is the browser-dependent default value of the button. It could be submit, or it could be Lähetä kysely, or something exotic. You can, and normally should, use the value attribute to set this value, since it determines the text displayed in the button. It’s usually not desirable to have a submit button on your English-language appear with e.g. some text in Japanese just because a Japanese-language browser is being used.
So as others have written, the solution (if this is a problem) is to remove the name attribute. But since the value attribute should normally be used, you can make two changes simultaneously by just replacing the attribute name name by the name value, though you might also capitalize the word shown:
<input type="submit" value="Submit">
Try to remove name attribute from submit input
remove the name attribute of the button.....

Multiple submits in an HTML form

I have an HTML form that needs multiple submit buttons, like this:
<input type="submit" name="foo" value="1"/>
<input type="submit" name="foo" value="2"/>
<input type="submit" name="foo" value="3"/>
The problem is that I want it to display on the button something other than what is in the value attribute (in the example above: 1, 2, 3). For example, I want to show "Bar" for the button with value="1". Is this possible?
I've considered using the <button> tag, like this:
<button name="foo" value="1">Bar</button>
The problem with using <button> (from w3schools):
If you use the element in an HTML form, different browsers
may submit different values. Internet Explorer, prior version 9, will
submit the text between the and tags, while other
browsers will submit the content of the value attribute. Use the
element to create buttons in an HTML form.
Thoughts?
Give each of the buttons a unique name and then check for their existence in the POST vars instead. Then you can set the value to whatever you want.
Every button must have unique name so that you can set any value/values to one or more buttons. eg
<input type = "button" name = "foo1" value = "Bar1">
<input type = "button" name = "foo2" value = "Bar2">
<input type = "button" name = "foo3" value = "Bar3">
implement it..

HTML input field OUTSIDE of a form

I have a form with a set of inputs, and I want my page to refresh when one of them changes. I have a second set of inputs on the OTHER side of the page, and the css layout doesn't make it convenient for me to put them in the same <form> </form> tag. I was wondering if there is a way that I can make sure those "inputs" that are located outside of the <form> tag are still associated with that form.
Is there some way we can assign a "form id" to the inputs?
In HTML5, you can use the form attribute:
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.
Example:
<form id="myform">
<input id="something" type="text">
</form>
<button form="myform" type="submit">Submit that form over there</button>
You should however make sure that it is clear to the user that these visually separated form elements are in fact connected.
<input type="text" form="myform" /> worked for me.
Update
This worked great with FireFox, however gave me trouble in IE 11 as the form attribute is not recognized with IE (as of writing this).
I had to just set a hidden input field inside the form, and transferred value to hidden input field from input outside form; with onclick using jQuery.
<input class="checkbox" id="staff_recruiting" name="company_type"
value="staff_recruiting" type="checkbox">
<input type="hidden" value="all" name="keyword" id="search-keyword-input">
$('#search-keyword').keyup(function() {
$('#search-keyword-input').val($(this).val());
});
Your problem will be solved bro:
Add a hidden input field in your form.
Using jQuery or JS to change that hidden input field value with that outside input box.
Your page will refresh and your outside box value will be grabbed.