Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does the submit button have to be contained in the form

I want my submit button to be positioned somewhere that outside my form element? Do I have any options? With the exception of jquery.

Thanks, rodchar

like image 862
Rod Avatar asked May 07 '10 03:05

Rod


People also ask

Can the submit button be outside the form?

For a HTML form element you can put your submit button outside of the form element as long as you have reference the form's id property with the button's form property.

Do buttons need to be in forms?

A button element is valid anywhere in the document body where text-level markup may appear. Such an element need not have any relationship to a form element.

Where should the submit button be?

Submit buttons are placed at the bottom of the page to optimize top to bottom flow. There should always be two buttons, a primary action button that will commit changes made by the user and a Cancel button that will abort those changes.

How do we define the submit button in a form?

The Submit Button The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.


2 Answers

Another approach to this is merely to set the form attribute on the button:

<form id="first">
    <input type="submit" form="second" value="Submit Second" />
</form>
<form id="second">
    <input type="submit" form="first" value="Submit First" />
</form>

Fiddle: http://jsfiddle.net/52wgc2ym/


Original Answer

The natural behavior of a submit button is to submit the nearest form up its hierarchy. The only other way to get a button to submit a form which it doesn't reside in is to use JavaScript (which is what jQuery is, basically).

If necessary, you could reposition the submit button so that it appears as though it's not in the form visually when the page is rendered. You would do this using CSS, which may give the desired result(s).

like image 149
Sampson Avatar answered Oct 02 '22 14:10

Sampson


This is a common situation. I think this will do it (haven't tested it):

<form id="form1" action="someAction.cgi" method="GET">
    <!-- other fields go here -->
</form>
<form id="form2" action="someOtherAction.cgi" method="GET">
    <!-- other fields go here -->
</form>

<form>
    <input value="Form One" type="button"
        onclick="document.getElementById('form1').submit();"/>
    <input value="Form Two" type="button"
        onclick="document.getElementById('form2').submit();"/>
</form>

I'm not sure if you need that last <form>. I seem to remember browsers ignoring events if the button wasn't in a form.

like image 42
egrunin Avatar answered Oct 02 '22 12:10

egrunin