Submit form with href="" instead of input="submit"

7

I was given a layout of a form:

<form method="post" action="" id="mail_form">
   <div id="newsletter"> 
      <input type="text" name="email_newsletter" />
      <div class="newslink">
         <input type="hidden" name="submit_newsletter" />
         <a href="">ENVIAR</a>
      </div>
   </div>
</form>

How can I make the form to be submitted from <a href=""> by clicking "SEND"?

I know there are other ways to send a form, but in this case, I would like to submit it as well.

    
asked by anonymous 01.04.2014 / 14:02

2 answers

12

In this way, only with javascript.

The correct method is to use a button of type <input type="submit"> , a <button type="submit"> or a <input type="image"> , the latter two have similar effect to submit, whereas button allows other nested elements (images included), and the second allows the sending by clicking on a specific image (by sending the coordinates clicked together).

If you still want a <a> , follow the code. I do not recommend it as it affects the accessibility of the form.

<form method="post" action="" id="mail_form">
    <div id="newsletter"> 
        <input type="text" name="email_newsletter" />
        <div class="newslink">
            <input type="hidden" name="submit_newsletter" />
            <a href="#" onClick="document.getElementById('mail_form').submit();">ENVIAR</a>
        </div>
    </div>
</form>
  

Remember that nowadays, using CSS, you can make the submit conventional look like a regular link, with the advantage of not "breaking" the normal functionality of form .

I would still have a gambiarra which is to use a <label for="botaodeenvio"> and hide the original button with position, opacity, etc, but it will still not be <a> . Better to use CSS and conventional methods.

    
01.04.2014 / 14:06
1

You can use simplified code like this javascript: x.submit ()

<a href='javascript:formName.submit()'>ENVIAR</a>

Where formName matches the form's name, for example:

<form name='pagamento' action=teste.html>
/*Conteúdo do formulário*/
</form>

And the link would look like this:

<a href='javascript:pagamento.submit()' target='_blank'>ENVIAR</a>
<form name='pagamento' action='teste.html' >
<input value='10' name='valor' type='hidden'/>
</form>
    
19.10.2016 / 15:28