Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - How to submit form onclick and send submit button

I need to submit form by button which is out of scope of the form by JavaSript.

<form>
  <input name="data" type="text">
  <input type="submit" name="send" id="send-button" style="display: none">
</form>

<button onclick="$('#send-button').click() || .submit()"></button>

The button #send-button is visible and I need to send whole form to server. It means I receive $data and $send (both).

Now I am getting just the $data.

Any idea?

like image 332
MakoBuk Avatar asked Sep 23 '15 11:09

MakoBuk


2 Answers

You can do with javascript form method

<button onclick="document.forms[0].submit()"></button>

if you have multiple forms in the document then set id to the form

<form id="form1">
  <input name="data" type="text">
  <input type="submit" name="send" id="send-button" style="display: none">
</form>
<button onclick="document.getElementById("form1").submit()"></button>
like image 99
Man Programmer Avatar answered Oct 07 '22 17:10

Man Programmer


Give the form a name:

    <form name="myform">
        <a href="javascript: submitform()">Submit</a>
    </form>

Submit form function:

    <script type="text/javascript">
        function submitform()
        {
           document.myform.submit();
        }
    </script>

Invoke submission:

    document.forms["myform"].submit();
like image 27
KurinchiMalar Avatar answered Oct 07 '22 18:10

KurinchiMalar