javascript - submit form automatically only once - Stack Overflow

I have made a form which I need to self submit automatically when page is opened.I have tried this jav

I have made a form which I need to self submit automatically when page is opened. I have tried this javascript code but what it does it creates a loop where form is submitted unlimited time without stop. I need the code where form would be submitted only once when page is opened. Please give me an advice.

<form id="form" action="form.php" method="POST" >
  <select id="main" name="main">
    <option value="1" <?php if (@$_POST['main']=='1') {echo "selected='selected'"; } ?>>One</option>
    <option value="2" <?php if (@$_POST['main']=='2') {echo "selected='selected'"; } ?>>Two</option>
  </select>
</form>

<script>
  document.getElementById("form").submit();
</script>

I have made a form which I need to self submit automatically when page is opened. I have tried this javascript code but what it does it creates a loop where form is submitted unlimited time without stop. I need the code where form would be submitted only once when page is opened. Please give me an advice.

<form id="form" action="form.php" method="POST" >
  <select id="main" name="main">
    <option value="1" <?php if (@$_POST['main']=='1') {echo "selected='selected'"; } ?>>One</option>
    <option value="2" <?php if (@$_POST['main']=='2') {echo "selected='selected'"; } ?>>Two</option>
  </select>
</form>

<script>
  document.getElementById("form").submit();
</script>
Share Improve this question asked Apr 11, 2014 at 7:17 user3284181user3284181 171 silver badge5 bronze badges 2
  • use cookies to store a value and check it next time – ɹɐqʞɐ zoɹǝɟ Commented Apr 11, 2014 at 7:19
  • After sending post for form, U are sending this again during loading... – Andrzej Reduta Commented Apr 11, 2014 at 7:20
Add a ment  | 

5 Answers 5

Reset to default 2

You can check if the form was previously submitted with the POST superglobal

<form id="form" action="form.php" method="POST" >
  <select id="main" name="main">
    <option value="1" <?php if (@$_POST['main']=='1') {echo "selected='selected'"; } ?>>One</option>
    <option value="2" <?php if (@$_POST['main']=='2') {echo "selected='selected'"; } ?>>Two</option>
  </select>
</form>

<?php
    if ( ! isset($_POST['main']) ) { // not submitted yet
?>
   <script>
      document.getElementById("form").submit();
   </script>
<?php
    }
?>

try calling on onload of document:

function submitForm()
{
   document.getElementById("form").submit();
}
window.onload=submitForm;

Since page onload called only once when page is pletely loaded so it will call it once. Until you are reloading page again by code or using refresh of browser.

Below is the java script for submit form automatically you have to import jquery.min.js from http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js

after reading a td value of class error

$("td.error").each(function () {
    // 'this' is now the raw td DOM element
    var txt = $(this).html();
    n=txt.length;   
});
if(n!=0)
    {
        alert(n)
    }
    if(n==0)
    {

        document.forms["login"].submit();
    }

my html file contain code like

<td colspan="2" class="error"><br/><i18n:text><xsl:value-of select="$message"/></i18n:text></td>

use this:

<body onload =  "if (location.search.length < 1){ document.getElementById('form').submit()}">

you can try onload event in body tag..

like <body onload="submitForm();">

and your function like

<script>
function submitForm()
{
   document.getElementById("form").submit();
}
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744309616a4567887.html

相关推荐

  • javascript - submit form automatically only once - Stack Overflow

    I have made a form which I need to self submit automatically when page is opened.I have tried this jav

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信