user2644795
user2644795

Reputation: 85

Automatic form submission using JS

I am developing a code, which takes a value as input and the form when submitted, displays the result and the code is as follows:

<script>
function submitme()
{

    document.forms["myform"].submit();
}
</script>
<body onload="submitme()">
<FORM name="performance" id="myform" method="post" action="http://www.pvpsiddhartha.ac.in/index.sit" >
        <INPUT type="hidden" name="service" value="STU_PERFORMANCE_MARKS_DISPLAY">
          <TABLE border="0" width="100%" cellspacing="0" cellpadding="0">
            <TR>
              <TD colspan="2" align="center"><STRONG><U>Student Marks Performance Display Form</U></STRONG></TD>
            </TR>  
            <TR><TD>&nbsp;</TD></TR>
            <TR>
              <TD align="right" width="40%">Regd. No:</TD>
              <TD><INPUT type="text" name="stid" value="<?php echo $_GET['x']; ?>"></TD>
            </TR>
            <TR><TD>&nbsp;</TD></TR>
            <TR>
              <TD align="right"><INPUT type="submit" name="submit" value="Submit"></TD>
              <TD align="center"><INPUT type="reset" value="Clear"></TD>
            </TR>
          </TABLE>
        </FORM>
        </body>

The problem is, when the page is loaded completely, the form should be submitted , but the form cannot be submitted. What is the problem , Help me!! Thanks in advance

Upvotes: 0

Views: 132

Answers (3)

Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

Try this pure javascript,

  <body>
    <FORM name="performance" id="myform" method="post" action="http://www.pvpsiddhartha.ac.in/index.sit" >
      <INPUT type="hidden" name="service" value="STU_PERFORMANCE_MARKS_DISPLAY">
        <TABLE border="0" width="100%" cellspacing="0" cellpadding="0">
          <TR>
            <TD colspan="2" align="center"><STRONG><U>Student Marks Performance Display Form</U></STRONG></TD>
          </TR>  
          <TR><TD>&nbsp;</TD></TR>
          <TR>
            <TD align="right" width="40%">Regd. No:</TD>
            <TD><INPUT type="text" name="stid" value=""></TD>
          </TR>
          <TR><TD>&nbsp;</TD></TR>
          <TR>
            <TD align="right"><INPUT type="submit" name="submit_button" value="Submit"></TD>
            <TD align="center"><INPUT type="reset" value="Clear"></TD>
          </TR>
        </TABLE>
      </FORM>

    <script>
        window.onload = function(){
          document.getElementById('myform').submit();
        };

    </script>

To access this document.getElementById('myform').submit(), you should change <INPUT type="submit" name="submit" value="Submit"> to <INPUT type="submit" name="submit_button" value="Submit">. Don't use other element with name of submit or id.

And also the better one is,

   <script>
    window.ready = function(){
      document.getElementById('myform').submit();
    };
  </script>

Upvotes: 3

Mr.LamYahoo
Mr.LamYahoo

Reputation: 1546

try

<script>
function submitme()
{

    document.forms["performance"].submit();
}
</script>

or

<script>
function submitme()
{

    document.forms[0].submit();
}
</script>

Upvotes: 1

Daemon025
Daemon025

Reputation: 87

It should help:

$(document).ready(function () {
    $('#myform').trigger('submit'); });

Still I don't get what is the point of submitting the form after page loads.

Upvotes: 1

Related Questions