4

I am new to JavaScript & php.

How can one submit a form without using submit button?

if possible without using anchor tag.

Please guide me how to achieve this task.

Anon30
  • 567
  • 1
  • 6
  • 21

4 Answers4

17

just use following code

<form id="jsform" action="whatever you want">
// input fields
</form>

<script type="text/javascript">
  document.getElementById('jsform').submit();
</script>
RiaD
  • 46,822
  • 11
  • 79
  • 123
  • 1
    it is not working. whenever page loads with above it directly go to the action page. it does not wait to take input.
    --------------------
    Name :
    – Anon30 Feb 12 '13 at 09:17
  • yes it will submit and page refresh when page load if you use it as –  Feb 12 '13 at 09:22
  • you need to bind it with any button events –  Feb 12 '13 at 09:22
  • like –  Feb 12 '13 at 09:23
  • 1
    If you have a submit button named 'submit', this will either quietly fail (jQuery) or produce error saying "submit is not a function" (vanilla js). [Renaming button to anything else will resolve](https://stackoverflow.com/questions/833032/submit-is-not-a-function-error-in-javascript). **¯\_(ツ)_/¯** – fzzylogic Jul 06 '19 at 04:12
7

you can submit it with the use of javascript submit() function

<script type="text/javascript">
function submitform()
{
document.forms["myform"].submit();
alert("Value is sumitted");
}
</script>

<form id="myform" action="submit.php" method="post">
Search: <input type="text" name="query">
<a href="javascript: submitform()">Submit</a>
</form>
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0

I think you can make your submit button's visibility 'hidden' and use the following javascript.

<script type="text/javascript">
// Using jQuery.

$(function() {
$('form').each(function() {
    $('input').keypress(function(e) {
        // Enter pressed?
        if(e.which == 10 || e.which == 13) {
            this.form.submit();
        }
    });

    $('input[type=submit]').hide();
   });
});
</script>


<form name="formName" method="post">
   <input name="someName" type="text" /><br />
   <input name="somePass" type="password" />
   <input type="submit" style="visibility: hidden;" />
</form>
0

In javascript it must use the form submit() method implemented in a custom button or anchor.

In PHP you can use the curl library (client URL library).