Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form using a button outside the <form> tag

Tags:

html

Say I have:

<form method="get" action="something.php">     <input type="text" name="name" /> </form>  <input type="submit" /> 

How do I go about submitting that form with that submit button outside of the form, I think in HTML5 theres an action attribute for the submit but I'm not sure if thats fully cross-browser and if it isn't is there anyway to do this?

like image 242
daryl Avatar asked Aug 11 '11 03:08

daryl


People also ask

Can a button outside a form submit the form?

You can tie a submit button to a form that the button doesn't live inside of. The trick is to give the form an id and then reference that id with the button's form property. With this setup, clicking the Submit button will cause the form to be submitted.

Can a button submit a form?

submit : The button submits the form data to the server. This is the default if the attribute is not specified for buttons associated with a <form> , or if the attribute is an empty or invalid value. reset : The button resets all the controls to their initial values, like <input type="reset">.

Can we submit form without form tag?

It is actually possible to "submit" a "form" without form-tags. what you need is an ajax-function, e.g. with jquery, which will get the values by id. But there is no reason not to use a form tho. you should use form tag.

Does submit need to be inside form?

Yes, structurally the submit button needs to be inside a form element for the document to be valid X/HTML.


2 Answers

In HTML5, there is the form attribute. Basically

<form id="myform" method="get" action="something.php">     <input type="text" name="name" /> </form>  <input type="submit" form="myform" value="Update"/> 
like image 67
Lie Ryan Avatar answered Sep 17 '22 12:09

Lie Ryan


A solution that works great for me, is still missing here. It requires having a visually hidden <submit> or <input type="submit"> element whithin the <form>, and an associated <label> element outside of it. It would look like this:

<form method="get" action="something.php">      <input type="text" name="name" />      <input type="submit" id="submit-form" class="hidden" /> </form>  <label for="submit-form" tabindex="0">Submit</label> 

Now this link enables you to 'click' the form <submit> element by clicking the <label> element.

like image 33
Offereins Avatar answered Sep 16 '22 12:09

Offereins