Vinod VT
Vinod VT

Reputation: 7149

How to submit an HTML form on loading the page?

How to submit this form without using submit button. I want to submit it in loading this form,

<form name="frm1" id="frm1" action="../somePage" method="post">
    Please Waite...
    <input type="hidden" name="uname" id="uname" value="<?php echo $uname;?>" />
    <input type="hidden" name="price" id="price" value="<?php echo $price;?>" />
</form>

Upvotes: 8

Views: 113346

Answers (6)

Shankar Prakash G
Shankar Prakash G

Reputation: 1129

You can do it by using simple one line JavaScript code and also be careful that if JavaScript is turned off it will not work. The below code will do it's job if JavaScript is turned off.

Turn off JavaScript and run the code on you own file to know it's full function.(If you turn off JavaScript here, the below Code Snippet will not work)

.noscript-error {
  color: red;
}
<body onload="document.getElementById('payment-form').submit();">

  <div align="center">
    <h1>
      Please Waite... You Will be Redirected Shortly<br/>
      Don't Refresh or Press Back 
    </h1>
  </div>

  <form method='post' action='acction.php' id='payment-form'>
    <input type='hidden' name='field-name' value='field-value'>
     <input type='hidden' name='field-name2' value='field-value2'>
    <noscript>
      <div align="center" class="noscript-error">Sorry, your browser does not support JavaScript!.
        <br>Kindly submit it manually
        <input type='submit' value='Submit Now' />
      </div>
    </noscript>
  </form>

</body>

Upvotes: 5

HNygard
HNygard

Reputation: 4796

You don't need Jquery here! The simplest solution here is (based on the answer from charles):

<html>
<body onload="document.frm1.submit()">
   <form action="http://www.google.com" name="frm1">
      <input type="hidden" name="q" value="Hello world" />
   </form>
</body>
</html>

Upvotes: 49

charles
charles

Reputation: 111

You can try also using below script

<html>
<head>
<script>
function load()
{
document.frm1.submit()
}
</script>
</head>

<body onload="load()">
<form action="http://www.google.com" id="frm1" name="frm1">
<input type="text" value="" />
</form>
</body>
</html> 

Upvotes: 10

Deepak Srinivasan
Deepak Srinivasan

Reputation: 774

using javascript

 <form id="frm1" action="file.php"></form>
    <script>document.getElementById('frm1').submit();</script>

Upvotes: 3

Ramesh Moorthy
Ramesh Moorthy

Reputation: 689

You missed the closing tag for the input fields, and you can choose any one of the events, ex: onload, onclick etc.

(a) Onload event:

<script type="text/javascript">
$(document).ready(function(){
     $('#frm1').submit();
});
</script>

(b) Onclick Event:

<form name="frm1" id="frm1" action="../somePage" method="post">
    Please Waite... 
    <input type="hidden" name="uname" id="uname" value=<?php echo $uname;?> />
    <input type="hidden" name="price" id="price" value=<?php echo $price;?> />
    <input type="text" name="submit" id="submit" value="submit">
</form>
<script type="text/javascript">
$('#submit').click(function(){
     $('#frm1').submit();
});
</script>

Upvotes: 3

Ravinder Singh
Ravinder Singh

Reputation: 3133

Do this :

$(document).ready(function(){
     $("#frm1").submit();
});

Upvotes: 6

Related Questions